nfs4proc.c 259 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677
  1. /*
  2. * fs/nfs/nfs4proc.c
  3. *
  4. * Client-side procedure declarations for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/mm.h>
  38. #include <linux/delay.h>
  39. #include <linux/errno.h>
  40. #include <linux/string.h>
  41. #include <linux/ratelimit.h>
  42. #include <linux/printk.h>
  43. #include <linux/slab.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/nfs.h>
  46. #include <linux/nfs4.h>
  47. #include <linux/nfs_fs.h>
  48. #include <linux/nfs_page.h>
  49. #include <linux/nfs_mount.h>
  50. #include <linux/namei.h>
  51. #include <linux/mount.h>
  52. #include <linux/module.h>
  53. #include <linux/xattr.h>
  54. #include <linux/utsname.h>
  55. #include <linux/freezer.h>
  56. #include <linux/iversion.h>
  57. #include "nfs4_fs.h"
  58. #include "delegation.h"
  59. #include "internal.h"
  60. #include "iostat.h"
  61. #include "callback.h"
  62. #include "pnfs.h"
  63. #include "netns.h"
  64. #include "nfs4idmap.h"
  65. #include "nfs4session.h"
  66. #include "fscache.h"
  67. #include "nfs4trace.h"
  68. #define NFSDBG_FACILITY NFSDBG_PROC
  69. #define NFS4_BITMASK_SZ 3
  70. #define NFS4_POLL_RETRY_MIN (HZ/10)
  71. #define NFS4_POLL_RETRY_MAX (15*HZ)
  72. /* file attributes which can be mapped to nfs attributes */
  73. #define NFS4_VALID_ATTRS (ATTR_MODE \
  74. | ATTR_UID \
  75. | ATTR_GID \
  76. | ATTR_SIZE \
  77. | ATTR_ATIME \
  78. | ATTR_MTIME \
  79. | ATTR_CTIME \
  80. | ATTR_ATIME_SET \
  81. | ATTR_MTIME_SET)
  82. struct nfs4_opendata;
  83. static void nfs4_layoutget_release(void *calldata);
  84. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  85. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  86. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  87. static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
  88. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
  89. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  90. struct nfs_fattr *fattr, struct iattr *sattr,
  91. struct nfs_open_context *ctx, struct nfs4_label *ilabel,
  92. struct nfs4_label *olabel);
  93. #ifdef CONFIG_NFS_V4_1
  94. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  95. struct rpc_cred *cred,
  96. struct nfs4_slot *slot,
  97. bool is_privileged);
  98. static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
  99. struct rpc_cred *);
  100. static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
  101. struct rpc_cred *, bool);
  102. #endif
  103. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  104. static inline struct nfs4_label *
  105. nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
  106. struct iattr *sattr, struct nfs4_label *label)
  107. {
  108. int err;
  109. if (label == NULL)
  110. return NULL;
  111. if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
  112. return NULL;
  113. err = security_dentry_init_security(dentry, sattr->ia_mode,
  114. &dentry->d_name, (void **)&label->label, &label->len);
  115. if (err == 0)
  116. return label;
  117. return NULL;
  118. }
  119. static inline void
  120. nfs4_label_release_security(struct nfs4_label *label)
  121. {
  122. if (label)
  123. security_release_secctx(label->label, label->len);
  124. }
  125. static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
  126. {
  127. if (label)
  128. return server->attr_bitmask;
  129. return server->attr_bitmask_nl;
  130. }
  131. #else
  132. static inline struct nfs4_label *
  133. nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
  134. struct iattr *sattr, struct nfs4_label *l)
  135. { return NULL; }
  136. static inline void
  137. nfs4_label_release_security(struct nfs4_label *label)
  138. { return; }
  139. static inline u32 *
  140. nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
  141. { return server->attr_bitmask; }
  142. #endif
  143. /* Prevent leaks of NFSv4 errors into userland */
  144. static int nfs4_map_errors(int err)
  145. {
  146. if (err >= -1000)
  147. return err;
  148. switch (err) {
  149. case -NFS4ERR_RESOURCE:
  150. case -NFS4ERR_LAYOUTTRYLATER:
  151. case -NFS4ERR_RECALLCONFLICT:
  152. return -EREMOTEIO;
  153. case -NFS4ERR_WRONGSEC:
  154. case -NFS4ERR_WRONG_CRED:
  155. return -EPERM;
  156. case -NFS4ERR_BADOWNER:
  157. case -NFS4ERR_BADNAME:
  158. return -EINVAL;
  159. case -NFS4ERR_SHARE_DENIED:
  160. return -EACCES;
  161. case -NFS4ERR_MINOR_VERS_MISMATCH:
  162. return -EPROTONOSUPPORT;
  163. case -NFS4ERR_FILE_OPEN:
  164. return -EBUSY;
  165. default:
  166. dprintk("%s could not handle NFSv4 error %d\n",
  167. __func__, -err);
  168. break;
  169. }
  170. return -EIO;
  171. }
  172. /*
  173. * This is our standard bitmap for GETATTR requests.
  174. */
  175. const u32 nfs4_fattr_bitmap[3] = {
  176. FATTR4_WORD0_TYPE
  177. | FATTR4_WORD0_CHANGE
  178. | FATTR4_WORD0_SIZE
  179. | FATTR4_WORD0_FSID
  180. | FATTR4_WORD0_FILEID,
  181. FATTR4_WORD1_MODE
  182. | FATTR4_WORD1_NUMLINKS
  183. | FATTR4_WORD1_OWNER
  184. | FATTR4_WORD1_OWNER_GROUP
  185. | FATTR4_WORD1_RAWDEV
  186. | FATTR4_WORD1_SPACE_USED
  187. | FATTR4_WORD1_TIME_ACCESS
  188. | FATTR4_WORD1_TIME_METADATA
  189. | FATTR4_WORD1_TIME_MODIFY
  190. | FATTR4_WORD1_MOUNTED_ON_FILEID,
  191. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  192. FATTR4_WORD2_SECURITY_LABEL
  193. #endif
  194. };
  195. static const u32 nfs4_pnfs_open_bitmap[3] = {
  196. FATTR4_WORD0_TYPE
  197. | FATTR4_WORD0_CHANGE
  198. | FATTR4_WORD0_SIZE
  199. | FATTR4_WORD0_FSID
  200. | FATTR4_WORD0_FILEID,
  201. FATTR4_WORD1_MODE
  202. | FATTR4_WORD1_NUMLINKS
  203. | FATTR4_WORD1_OWNER
  204. | FATTR4_WORD1_OWNER_GROUP
  205. | FATTR4_WORD1_RAWDEV
  206. | FATTR4_WORD1_SPACE_USED
  207. | FATTR4_WORD1_TIME_ACCESS
  208. | FATTR4_WORD1_TIME_METADATA
  209. | FATTR4_WORD1_TIME_MODIFY,
  210. FATTR4_WORD2_MDSTHRESHOLD
  211. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  212. | FATTR4_WORD2_SECURITY_LABEL
  213. #endif
  214. };
  215. static const u32 nfs4_open_noattr_bitmap[3] = {
  216. FATTR4_WORD0_TYPE
  217. | FATTR4_WORD0_FILEID,
  218. };
  219. const u32 nfs4_statfs_bitmap[3] = {
  220. FATTR4_WORD0_FILES_AVAIL
  221. | FATTR4_WORD0_FILES_FREE
  222. | FATTR4_WORD0_FILES_TOTAL,
  223. FATTR4_WORD1_SPACE_AVAIL
  224. | FATTR4_WORD1_SPACE_FREE
  225. | FATTR4_WORD1_SPACE_TOTAL
  226. };
  227. const u32 nfs4_pathconf_bitmap[3] = {
  228. FATTR4_WORD0_MAXLINK
  229. | FATTR4_WORD0_MAXNAME,
  230. 0
  231. };
  232. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  233. | FATTR4_WORD0_MAXREAD
  234. | FATTR4_WORD0_MAXWRITE
  235. | FATTR4_WORD0_LEASE_TIME,
  236. FATTR4_WORD1_TIME_DELTA
  237. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  238. FATTR4_WORD2_LAYOUT_BLKSIZE
  239. | FATTR4_WORD2_CLONE_BLKSIZE
  240. };
  241. const u32 nfs4_fs_locations_bitmap[3] = {
  242. FATTR4_WORD0_CHANGE
  243. | FATTR4_WORD0_SIZE
  244. | FATTR4_WORD0_FSID
  245. | FATTR4_WORD0_FILEID
  246. | FATTR4_WORD0_FS_LOCATIONS,
  247. FATTR4_WORD1_OWNER
  248. | FATTR4_WORD1_OWNER_GROUP
  249. | FATTR4_WORD1_RAWDEV
  250. | FATTR4_WORD1_SPACE_USED
  251. | FATTR4_WORD1_TIME_ACCESS
  252. | FATTR4_WORD1_TIME_METADATA
  253. | FATTR4_WORD1_TIME_MODIFY
  254. | FATTR4_WORD1_MOUNTED_ON_FILEID,
  255. };
  256. static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
  257. struct inode *inode)
  258. {
  259. unsigned long cache_validity;
  260. memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
  261. if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
  262. return;
  263. cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  264. if (!(cache_validity & NFS_INO_REVAL_FORCED))
  265. cache_validity &= ~(NFS_INO_INVALID_CHANGE
  266. | NFS_INO_INVALID_SIZE);
  267. if (!(cache_validity & NFS_INO_INVALID_SIZE))
  268. dst[0] &= ~FATTR4_WORD0_SIZE;
  269. if (!(cache_validity & NFS_INO_INVALID_CHANGE))
  270. dst[0] &= ~FATTR4_WORD0_CHANGE;
  271. }
  272. static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
  273. const __u32 *src, struct inode *inode)
  274. {
  275. nfs4_bitmap_copy_adjust(dst, src, inode);
  276. }
  277. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  278. struct nfs4_readdir_arg *readdir)
  279. {
  280. unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
  281. __be32 *start, *p;
  282. if (cookie > 2) {
  283. readdir->cookie = cookie;
  284. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  285. return;
  286. }
  287. readdir->cookie = 0;
  288. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  289. if (cookie == 2)
  290. return;
  291. /*
  292. * NFSv4 servers do not return entries for '.' and '..'
  293. * Therefore, we fake these entries here. We let '.'
  294. * have cookie 0 and '..' have cookie 1. Note that
  295. * when talking to the server, we always send cookie 0
  296. * instead of 1 or 2.
  297. */
  298. start = p = kmap_atomic(*readdir->pages);
  299. if (cookie == 0) {
  300. *p++ = xdr_one; /* next */
  301. *p++ = xdr_zero; /* cookie, first word */
  302. *p++ = xdr_one; /* cookie, second word */
  303. *p++ = xdr_one; /* entry len */
  304. memcpy(p, ".\0\0\0", 4); /* entry */
  305. p++;
  306. *p++ = xdr_one; /* bitmap length */
  307. *p++ = htonl(attrs); /* bitmap */
  308. *p++ = htonl(12); /* attribute buffer length */
  309. *p++ = htonl(NF4DIR);
  310. p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
  311. }
  312. *p++ = xdr_one; /* next */
  313. *p++ = xdr_zero; /* cookie, first word */
  314. *p++ = xdr_two; /* cookie, second word */
  315. *p++ = xdr_two; /* entry len */
  316. memcpy(p, "..\0\0", 4); /* entry */
  317. p++;
  318. *p++ = xdr_one; /* bitmap length */
  319. *p++ = htonl(attrs); /* bitmap */
  320. *p++ = htonl(12); /* attribute buffer length */
  321. *p++ = htonl(NF4DIR);
  322. p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
  323. readdir->pgbase = (char *)p - (char *)start;
  324. readdir->count -= readdir->pgbase;
  325. kunmap_atomic(start);
  326. }
  327. static void nfs4_test_and_free_stateid(struct nfs_server *server,
  328. nfs4_stateid *stateid,
  329. struct rpc_cred *cred)
  330. {
  331. const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
  332. ops->test_and_free_expired(server, stateid, cred);
  333. }
  334. static void __nfs4_free_revoked_stateid(struct nfs_server *server,
  335. nfs4_stateid *stateid,
  336. struct rpc_cred *cred)
  337. {
  338. stateid->type = NFS4_REVOKED_STATEID_TYPE;
  339. nfs4_test_and_free_stateid(server, stateid, cred);
  340. }
  341. static void nfs4_free_revoked_stateid(struct nfs_server *server,
  342. const nfs4_stateid *stateid,
  343. struct rpc_cred *cred)
  344. {
  345. nfs4_stateid tmp;
  346. nfs4_stateid_copy(&tmp, stateid);
  347. __nfs4_free_revoked_stateid(server, &tmp, cred);
  348. }
  349. static long nfs4_update_delay(long *timeout)
  350. {
  351. long ret;
  352. if (!timeout)
  353. return NFS4_POLL_RETRY_MAX;
  354. if (*timeout <= 0)
  355. *timeout = NFS4_POLL_RETRY_MIN;
  356. if (*timeout > NFS4_POLL_RETRY_MAX)
  357. *timeout = NFS4_POLL_RETRY_MAX;
  358. ret = *timeout;
  359. *timeout <<= 1;
  360. return ret;
  361. }
  362. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  363. {
  364. int res = 0;
  365. might_sleep();
  366. freezable_schedule_timeout_killable_unsafe(
  367. nfs4_update_delay(timeout));
  368. if (fatal_signal_pending(current))
  369. res = -ERESTARTSYS;
  370. return res;
  371. }
  372. /* This is the error handling routine for processes that are allowed
  373. * to sleep.
  374. */
  375. static int nfs4_do_handle_exception(struct nfs_server *server,
  376. int errorcode, struct nfs4_exception *exception)
  377. {
  378. struct nfs_client *clp = server->nfs_client;
  379. struct nfs4_state *state = exception->state;
  380. const nfs4_stateid *stateid = exception->stateid;
  381. struct inode *inode = exception->inode;
  382. int ret = errorcode;
  383. exception->delay = 0;
  384. exception->recovering = 0;
  385. exception->retry = 0;
  386. if (stateid == NULL && state != NULL)
  387. stateid = &state->stateid;
  388. switch(errorcode) {
  389. case 0:
  390. return 0;
  391. case -NFS4ERR_BADHANDLE:
  392. case -ESTALE:
  393. if (inode != NULL && S_ISREG(inode->i_mode))
  394. pnfs_destroy_layout(NFS_I(inode));
  395. break;
  396. case -NFS4ERR_DELEG_REVOKED:
  397. case -NFS4ERR_ADMIN_REVOKED:
  398. case -NFS4ERR_EXPIRED:
  399. case -NFS4ERR_BAD_STATEID:
  400. if (inode != NULL && stateid != NULL) {
  401. nfs_inode_find_state_and_recover(inode,
  402. stateid);
  403. goto wait_on_recovery;
  404. }
  405. case -NFS4ERR_OPENMODE:
  406. if (inode) {
  407. int err;
  408. err = nfs_async_inode_return_delegation(inode,
  409. stateid);
  410. if (err == 0)
  411. goto wait_on_recovery;
  412. if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
  413. exception->retry = 1;
  414. break;
  415. }
  416. }
  417. if (state == NULL)
  418. break;
  419. ret = nfs4_schedule_stateid_recovery(server, state);
  420. if (ret < 0)
  421. break;
  422. goto wait_on_recovery;
  423. case -NFS4ERR_STALE_STATEID:
  424. case -NFS4ERR_STALE_CLIENTID:
  425. nfs4_schedule_lease_recovery(clp);
  426. goto wait_on_recovery;
  427. case -NFS4ERR_MOVED:
  428. ret = nfs4_schedule_migration_recovery(server);
  429. if (ret < 0)
  430. break;
  431. goto wait_on_recovery;
  432. case -NFS4ERR_LEASE_MOVED:
  433. nfs4_schedule_lease_moved_recovery(clp);
  434. goto wait_on_recovery;
  435. #if defined(CONFIG_NFS_V4_1)
  436. case -NFS4ERR_BADSESSION:
  437. case -NFS4ERR_BADSLOT:
  438. case -NFS4ERR_BAD_HIGH_SLOT:
  439. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  440. case -NFS4ERR_DEADSESSION:
  441. case -NFS4ERR_SEQ_FALSE_RETRY:
  442. case -NFS4ERR_SEQ_MISORDERED:
  443. dprintk("%s ERROR: %d Reset session\n", __func__,
  444. errorcode);
  445. nfs4_schedule_session_recovery(clp->cl_session, errorcode);
  446. goto wait_on_recovery;
  447. #endif /* defined(CONFIG_NFS_V4_1) */
  448. case -NFS4ERR_FILE_OPEN:
  449. if (exception->timeout > HZ) {
  450. /* We have retried a decent amount, time to
  451. * fail
  452. */
  453. ret = -EBUSY;
  454. break;
  455. }
  456. case -NFS4ERR_DELAY:
  457. nfs_inc_server_stats(server, NFSIOS_DELAY);
  458. case -NFS4ERR_GRACE:
  459. case -NFS4ERR_LAYOUTTRYLATER:
  460. case -NFS4ERR_RECALLCONFLICT:
  461. exception->delay = 1;
  462. return 0;
  463. case -NFS4ERR_RETRY_UNCACHED_REP:
  464. case -NFS4ERR_OLD_STATEID:
  465. exception->retry = 1;
  466. break;
  467. case -NFS4ERR_BADOWNER:
  468. /* The following works around a Linux server bug! */
  469. case -NFS4ERR_BADNAME:
  470. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  471. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  472. exception->retry = 1;
  473. printk(KERN_WARNING "NFS: v4 server %s "
  474. "does not accept raw "
  475. "uid/gids. "
  476. "Reenabling the idmapper.\n",
  477. server->nfs_client->cl_hostname);
  478. }
  479. }
  480. /* We failed to handle the error */
  481. return nfs4_map_errors(ret);
  482. wait_on_recovery:
  483. exception->recovering = 1;
  484. return 0;
  485. }
  486. /* This is the error handling routine for processes that are allowed
  487. * to sleep.
  488. */
  489. int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  490. {
  491. struct nfs_client *clp = server->nfs_client;
  492. int ret;
  493. ret = nfs4_do_handle_exception(server, errorcode, exception);
  494. if (exception->delay) {
  495. ret = nfs4_delay(server->client, &exception->timeout);
  496. goto out_retry;
  497. }
  498. if (exception->recovering) {
  499. ret = nfs4_wait_clnt_recover(clp);
  500. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  501. return -EIO;
  502. goto out_retry;
  503. }
  504. return ret;
  505. out_retry:
  506. if (ret == 0)
  507. exception->retry = 1;
  508. return ret;
  509. }
  510. static int
  511. nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
  512. int errorcode, struct nfs4_exception *exception)
  513. {
  514. struct nfs_client *clp = server->nfs_client;
  515. int ret;
  516. ret = nfs4_do_handle_exception(server, errorcode, exception);
  517. if (exception->delay) {
  518. rpc_delay(task, nfs4_update_delay(&exception->timeout));
  519. goto out_retry;
  520. }
  521. if (exception->recovering) {
  522. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  523. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  524. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  525. goto out_retry;
  526. }
  527. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  528. ret = -EIO;
  529. return ret;
  530. out_retry:
  531. if (ret == 0)
  532. exception->retry = 1;
  533. return ret;
  534. }
  535. static int
  536. nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
  537. struct nfs4_state *state, long *timeout)
  538. {
  539. struct nfs4_exception exception = {
  540. .state = state,
  541. };
  542. if (task->tk_status >= 0)
  543. return 0;
  544. if (timeout)
  545. exception.timeout = *timeout;
  546. task->tk_status = nfs4_async_handle_exception(task, server,
  547. task->tk_status,
  548. &exception);
  549. if (exception.delay && timeout)
  550. *timeout = exception.timeout;
  551. if (exception.retry)
  552. return -EAGAIN;
  553. return 0;
  554. }
  555. /*
  556. * Return 'true' if 'clp' is using an rpc_client that is integrity protected
  557. * or 'false' otherwise.
  558. */
  559. static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
  560. {
  561. rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
  562. return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
  563. }
  564. static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  565. {
  566. spin_lock(&clp->cl_lock);
  567. if (time_before(clp->cl_last_renewal,timestamp))
  568. clp->cl_last_renewal = timestamp;
  569. spin_unlock(&clp->cl_lock);
  570. }
  571. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  572. {
  573. struct nfs_client *clp = server->nfs_client;
  574. if (!nfs4_has_session(clp))
  575. do_renew_lease(clp, timestamp);
  576. }
  577. struct nfs4_call_sync_data {
  578. const struct nfs_server *seq_server;
  579. struct nfs4_sequence_args *seq_args;
  580. struct nfs4_sequence_res *seq_res;
  581. };
  582. void nfs4_init_sequence(struct nfs4_sequence_args *args,
  583. struct nfs4_sequence_res *res, int cache_reply,
  584. int privileged)
  585. {
  586. args->sa_slot = NULL;
  587. args->sa_cache_this = cache_reply;
  588. args->sa_privileged = privileged;
  589. res->sr_slot = NULL;
  590. }
  591. static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
  592. {
  593. struct nfs4_slot *slot = res->sr_slot;
  594. struct nfs4_slot_table *tbl;
  595. tbl = slot->table;
  596. spin_lock(&tbl->slot_tbl_lock);
  597. if (!nfs41_wake_and_assign_slot(tbl, slot))
  598. nfs4_free_slot(tbl, slot);
  599. spin_unlock(&tbl->slot_tbl_lock);
  600. res->sr_slot = NULL;
  601. }
  602. static int nfs40_sequence_done(struct rpc_task *task,
  603. struct nfs4_sequence_res *res)
  604. {
  605. if (res->sr_slot != NULL)
  606. nfs40_sequence_free_slot(res);
  607. return 1;
  608. }
  609. #if defined(CONFIG_NFS_V4_1)
  610. static void nfs41_release_slot(struct nfs4_slot *slot)
  611. {
  612. struct nfs4_session *session;
  613. struct nfs4_slot_table *tbl;
  614. bool send_new_highest_used_slotid = false;
  615. if (!slot)
  616. return;
  617. tbl = slot->table;
  618. session = tbl->session;
  619. /* Bump the slot sequence number */
  620. if (slot->seq_done)
  621. slot->seq_nr++;
  622. slot->seq_done = 0;
  623. spin_lock(&tbl->slot_tbl_lock);
  624. /* Be nice to the server: try to ensure that the last transmitted
  625. * value for highest_user_slotid <= target_highest_slotid
  626. */
  627. if (tbl->highest_used_slotid > tbl->target_highest_slotid)
  628. send_new_highest_used_slotid = true;
  629. if (nfs41_wake_and_assign_slot(tbl, slot)) {
  630. send_new_highest_used_slotid = false;
  631. goto out_unlock;
  632. }
  633. nfs4_free_slot(tbl, slot);
  634. if (tbl->highest_used_slotid != NFS4_NO_SLOT)
  635. send_new_highest_used_slotid = false;
  636. out_unlock:
  637. spin_unlock(&tbl->slot_tbl_lock);
  638. if (send_new_highest_used_slotid)
  639. nfs41_notify_server(session->clp);
  640. if (waitqueue_active(&tbl->slot_waitq))
  641. wake_up_all(&tbl->slot_waitq);
  642. }
  643. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  644. {
  645. nfs41_release_slot(res->sr_slot);
  646. res->sr_slot = NULL;
  647. }
  648. static int nfs41_sequence_process(struct rpc_task *task,
  649. struct nfs4_sequence_res *res)
  650. {
  651. struct nfs4_session *session;
  652. struct nfs4_slot *slot = res->sr_slot;
  653. struct nfs_client *clp;
  654. bool interrupted = false;
  655. int ret = 1;
  656. if (slot == NULL)
  657. goto out_noaction;
  658. /* don't increment the sequence number if the task wasn't sent */
  659. if (!RPC_WAS_SENT(task))
  660. goto out;
  661. session = slot->table->session;
  662. if (slot->interrupted) {
  663. if (res->sr_status != -NFS4ERR_DELAY)
  664. slot->interrupted = 0;
  665. interrupted = true;
  666. }
  667. trace_nfs4_sequence_done(session, res);
  668. /* Check the SEQUENCE operation status */
  669. switch (res->sr_status) {
  670. case 0:
  671. /* Update the slot's sequence and clientid lease timer */
  672. slot->seq_done = 1;
  673. clp = session->clp;
  674. do_renew_lease(clp, res->sr_timestamp);
  675. /* Check sequence flags */
  676. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
  677. !!slot->privileged);
  678. nfs41_update_target_slotid(slot->table, slot, res);
  679. break;
  680. case 1:
  681. /*
  682. * sr_status remains 1 if an RPC level error occurred.
  683. * The server may or may not have processed the sequence
  684. * operation..
  685. * Mark the slot as having hosted an interrupted RPC call.
  686. */
  687. slot->interrupted = 1;
  688. goto out;
  689. case -NFS4ERR_DELAY:
  690. /* The server detected a resend of the RPC call and
  691. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  692. * of RFC5661.
  693. */
  694. dprintk("%s: slot=%u seq=%u: Operation in progress\n",
  695. __func__,
  696. slot->slot_nr,
  697. slot->seq_nr);
  698. goto out_retry;
  699. case -NFS4ERR_BADSLOT:
  700. /*
  701. * The slot id we used was probably retired. Try again
  702. * using a different slot id.
  703. */
  704. if (slot->seq_nr < slot->table->target_highest_slotid)
  705. goto session_recover;
  706. goto retry_nowait;
  707. case -NFS4ERR_SEQ_MISORDERED:
  708. /*
  709. * Was the last operation on this sequence interrupted?
  710. * If so, retry after bumping the sequence number.
  711. */
  712. if (interrupted)
  713. goto retry_new_seq;
  714. /*
  715. * Could this slot have been previously retired?
  716. * If so, then the server may be expecting seq_nr = 1!
  717. */
  718. if (slot->seq_nr != 1) {
  719. slot->seq_nr = 1;
  720. goto retry_nowait;
  721. }
  722. goto session_recover;
  723. case -NFS4ERR_SEQ_FALSE_RETRY:
  724. if (interrupted)
  725. goto retry_new_seq;
  726. goto session_recover;
  727. default:
  728. /* Just update the slot sequence no. */
  729. slot->seq_done = 1;
  730. }
  731. out:
  732. /* The session may be reset by one of the error handlers. */
  733. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  734. out_noaction:
  735. return ret;
  736. session_recover:
  737. nfs4_schedule_session_recovery(session, res->sr_status);
  738. goto retry_nowait;
  739. retry_new_seq:
  740. ++slot->seq_nr;
  741. retry_nowait:
  742. if (rpc_restart_call_prepare(task)) {
  743. nfs41_sequence_free_slot(res);
  744. task->tk_status = 0;
  745. ret = 0;
  746. }
  747. goto out;
  748. out_retry:
  749. if (!rpc_restart_call(task))
  750. goto out;
  751. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  752. return 0;
  753. }
  754. int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  755. {
  756. if (!nfs41_sequence_process(task, res))
  757. return 0;
  758. if (res->sr_slot != NULL)
  759. nfs41_sequence_free_slot(res);
  760. return 1;
  761. }
  762. EXPORT_SYMBOL_GPL(nfs41_sequence_done);
  763. static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
  764. {
  765. if (res->sr_slot == NULL)
  766. return 1;
  767. if (res->sr_slot->table->session != NULL)
  768. return nfs41_sequence_process(task, res);
  769. return nfs40_sequence_done(task, res);
  770. }
  771. static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
  772. {
  773. if (res->sr_slot != NULL) {
  774. if (res->sr_slot->table->session != NULL)
  775. nfs41_sequence_free_slot(res);
  776. else
  777. nfs40_sequence_free_slot(res);
  778. }
  779. }
  780. int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  781. {
  782. if (res->sr_slot == NULL)
  783. return 1;
  784. if (!res->sr_slot->table->session)
  785. return nfs40_sequence_done(task, res);
  786. return nfs41_sequence_done(task, res);
  787. }
  788. EXPORT_SYMBOL_GPL(nfs4_sequence_done);
  789. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  790. {
  791. struct nfs4_call_sync_data *data = calldata;
  792. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  793. nfs4_setup_sequence(data->seq_server->nfs_client,
  794. data->seq_args, data->seq_res, task);
  795. }
  796. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  797. {
  798. struct nfs4_call_sync_data *data = calldata;
  799. nfs41_sequence_done(task, data->seq_res);
  800. }
  801. static const struct rpc_call_ops nfs41_call_sync_ops = {
  802. .rpc_call_prepare = nfs41_call_sync_prepare,
  803. .rpc_call_done = nfs41_call_sync_done,
  804. };
  805. static void
  806. nfs4_sequence_process_interrupted(struct nfs_client *client,
  807. struct nfs4_slot *slot, struct rpc_cred *cred)
  808. {
  809. struct rpc_task *task;
  810. task = _nfs41_proc_sequence(client, cred, slot, true);
  811. if (!IS_ERR(task))
  812. rpc_put_task_async(task);
  813. }
  814. #else /* !CONFIG_NFS_V4_1 */
  815. static void nfs4_layoutget_release(void *calldata)
  816. {
  817. }
  818. static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
  819. {
  820. return nfs40_sequence_done(task, res);
  821. }
  822. static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
  823. {
  824. if (res->sr_slot != NULL)
  825. nfs40_sequence_free_slot(res);
  826. }
  827. int nfs4_sequence_done(struct rpc_task *task,
  828. struct nfs4_sequence_res *res)
  829. {
  830. return nfs40_sequence_done(task, res);
  831. }
  832. EXPORT_SYMBOL_GPL(nfs4_sequence_done);
  833. static void
  834. nfs4_sequence_process_interrupted(struct nfs_client *client,
  835. struct nfs4_slot *slot, struct rpc_cred *cred)
  836. {
  837. WARN_ON_ONCE(1);
  838. slot->interrupted = 0;
  839. }
  840. #endif /* !CONFIG_NFS_V4_1 */
  841. static
  842. void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
  843. struct nfs4_sequence_res *res,
  844. struct nfs4_slot *slot)
  845. {
  846. if (!slot)
  847. return;
  848. slot->privileged = args->sa_privileged ? 1 : 0;
  849. args->sa_slot = slot;
  850. res->sr_slot = slot;
  851. res->sr_timestamp = jiffies;
  852. res->sr_status_flags = 0;
  853. res->sr_status = 1;
  854. }
  855. int nfs4_setup_sequence(struct nfs_client *client,
  856. struct nfs4_sequence_args *args,
  857. struct nfs4_sequence_res *res,
  858. struct rpc_task *task)
  859. {
  860. struct nfs4_session *session = nfs4_get_session(client);
  861. struct nfs4_slot_table *tbl = client->cl_slot_tbl;
  862. struct nfs4_slot *slot;
  863. /* slot already allocated? */
  864. if (res->sr_slot != NULL)
  865. goto out_start;
  866. if (session) {
  867. tbl = &session->fc_slot_table;
  868. task->tk_timeout = 0;
  869. }
  870. for (;;) {
  871. spin_lock(&tbl->slot_tbl_lock);
  872. /* The state manager will wait until the slot table is empty */
  873. if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
  874. goto out_sleep;
  875. slot = nfs4_alloc_slot(tbl);
  876. if (IS_ERR(slot)) {
  877. /* Try again in 1/4 second */
  878. if (slot == ERR_PTR(-ENOMEM))
  879. task->tk_timeout = HZ >> 2;
  880. goto out_sleep;
  881. }
  882. spin_unlock(&tbl->slot_tbl_lock);
  883. if (likely(!slot->interrupted))
  884. break;
  885. nfs4_sequence_process_interrupted(client,
  886. slot, task->tk_msg.rpc_cred);
  887. }
  888. nfs4_sequence_attach_slot(args, res, slot);
  889. trace_nfs4_setup_sequence(session, args);
  890. out_start:
  891. rpc_call_start(task);
  892. return 0;
  893. out_sleep:
  894. if (args->sa_privileged)
  895. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  896. NULL, RPC_PRIORITY_PRIVILEGED);
  897. else
  898. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  899. spin_unlock(&tbl->slot_tbl_lock);
  900. return -EAGAIN;
  901. }
  902. EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
  903. static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
  904. {
  905. struct nfs4_call_sync_data *data = calldata;
  906. nfs4_setup_sequence(data->seq_server->nfs_client,
  907. data->seq_args, data->seq_res, task);
  908. }
  909. static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
  910. {
  911. struct nfs4_call_sync_data *data = calldata;
  912. nfs4_sequence_done(task, data->seq_res);
  913. }
  914. static const struct rpc_call_ops nfs40_call_sync_ops = {
  915. .rpc_call_prepare = nfs40_call_sync_prepare,
  916. .rpc_call_done = nfs40_call_sync_done,
  917. };
  918. static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  919. struct nfs_server *server,
  920. struct rpc_message *msg,
  921. struct nfs4_sequence_args *args,
  922. struct nfs4_sequence_res *res)
  923. {
  924. int ret;
  925. struct rpc_task *task;
  926. struct nfs_client *clp = server->nfs_client;
  927. struct nfs4_call_sync_data data = {
  928. .seq_server = server,
  929. .seq_args = args,
  930. .seq_res = res,
  931. };
  932. struct rpc_task_setup task_setup = {
  933. .rpc_client = clnt,
  934. .rpc_message = msg,
  935. .callback_ops = clp->cl_mvops->call_sync_ops,
  936. .callback_data = &data
  937. };
  938. task = rpc_run_task(&task_setup);
  939. if (IS_ERR(task))
  940. ret = PTR_ERR(task);
  941. else {
  942. ret = task->tk_status;
  943. rpc_put_task(task);
  944. }
  945. return ret;
  946. }
  947. int nfs4_call_sync(struct rpc_clnt *clnt,
  948. struct nfs_server *server,
  949. struct rpc_message *msg,
  950. struct nfs4_sequence_args *args,
  951. struct nfs4_sequence_res *res,
  952. int cache_reply)
  953. {
  954. nfs4_init_sequence(args, res, cache_reply, 0);
  955. return nfs4_call_sync_sequence(clnt, server, msg, args, res);
  956. }
  957. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
  958. unsigned long timestamp)
  959. {
  960. struct nfs_inode *nfsi = NFS_I(dir);
  961. spin_lock(&dir->i_lock);
  962. nfsi->cache_validity |= NFS_INO_INVALID_CTIME
  963. | NFS_INO_INVALID_MTIME
  964. | NFS_INO_INVALID_DATA;
  965. if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
  966. nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
  967. nfsi->attrtimeo_timestamp = jiffies;
  968. } else {
  969. nfs_force_lookup_revalidate(dir);
  970. if (cinfo->before != inode_peek_iversion_raw(dir))
  971. nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
  972. NFS_INO_INVALID_ACL;
  973. }
  974. inode_set_iversion_raw(dir, cinfo->after);
  975. nfsi->read_cache_jiffies = timestamp;
  976. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  977. nfs_fscache_invalidate(dir);
  978. spin_unlock(&dir->i_lock);
  979. }
  980. struct nfs4_open_createattrs {
  981. struct nfs4_label *label;
  982. struct iattr *sattr;
  983. const __u32 verf[2];
  984. };
  985. static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
  986. int err, struct nfs4_exception *exception)
  987. {
  988. if (err != -EINVAL)
  989. return false;
  990. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  991. return false;
  992. server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
  993. exception->retry = 1;
  994. return true;
  995. }
  996. static u32
  997. nfs4_map_atomic_open_share(struct nfs_server *server,
  998. fmode_t fmode, int openflags)
  999. {
  1000. u32 res = 0;
  1001. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  1002. case FMODE_READ:
  1003. res = NFS4_SHARE_ACCESS_READ;
  1004. break;
  1005. case FMODE_WRITE:
  1006. res = NFS4_SHARE_ACCESS_WRITE;
  1007. break;
  1008. case FMODE_READ|FMODE_WRITE:
  1009. res = NFS4_SHARE_ACCESS_BOTH;
  1010. }
  1011. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  1012. goto out;
  1013. /* Want no delegation if we're using O_DIRECT */
  1014. if (openflags & O_DIRECT)
  1015. res |= NFS4_SHARE_WANT_NO_DELEG;
  1016. out:
  1017. return res;
  1018. }
  1019. static enum open_claim_type4
  1020. nfs4_map_atomic_open_claim(struct nfs_server *server,
  1021. enum open_claim_type4 claim)
  1022. {
  1023. if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
  1024. return claim;
  1025. switch (claim) {
  1026. default:
  1027. return claim;
  1028. case NFS4_OPEN_CLAIM_FH:
  1029. return NFS4_OPEN_CLAIM_NULL;
  1030. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1031. return NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1032. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1033. return NFS4_OPEN_CLAIM_DELEGATE_PREV;
  1034. }
  1035. }
  1036. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  1037. {
  1038. p->o_res.f_attr = &p->f_attr;
  1039. p->o_res.f_label = p->f_label;
  1040. p->o_res.seqid = p->o_arg.seqid;
  1041. p->c_res.seqid = p->c_arg.seqid;
  1042. p->o_res.server = p->o_arg.server;
  1043. p->o_res.access_request = p->o_arg.access;
  1044. nfs_fattr_init(&p->f_attr);
  1045. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  1046. }
  1047. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  1048. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  1049. const struct nfs4_open_createattrs *c,
  1050. enum open_claim_type4 claim,
  1051. gfp_t gfp_mask)
  1052. {
  1053. struct dentry *parent = dget_parent(dentry);
  1054. struct inode *dir = d_inode(parent);
  1055. struct nfs_server *server = NFS_SERVER(dir);
  1056. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  1057. struct nfs4_label *label = (c != NULL) ? c->label : NULL;
  1058. struct nfs4_opendata *p;
  1059. p = kzalloc(sizeof(*p), gfp_mask);
  1060. if (p == NULL)
  1061. goto err;
  1062. p->f_label = nfs4_label_alloc(server, gfp_mask);
  1063. if (IS_ERR(p->f_label))
  1064. goto err_free_p;
  1065. p->a_label = nfs4_label_alloc(server, gfp_mask);
  1066. if (IS_ERR(p->a_label))
  1067. goto err_free_f;
  1068. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  1069. p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
  1070. if (IS_ERR(p->o_arg.seqid))
  1071. goto err_free_label;
  1072. nfs_sb_active(dentry->d_sb);
  1073. p->dentry = dget(dentry);
  1074. p->dir = parent;
  1075. p->owner = sp;
  1076. atomic_inc(&sp->so_count);
  1077. p->o_arg.open_flags = flags;
  1078. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  1079. p->o_arg.umask = current_umask();
  1080. p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
  1081. p->o_arg.share_access = nfs4_map_atomic_open_share(server,
  1082. fmode, flags);
  1083. /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
  1084. * will return permission denied for all bits until close */
  1085. if (!(flags & O_EXCL)) {
  1086. /* ask server to check for all possible rights as results
  1087. * are cached */
  1088. switch (p->o_arg.claim) {
  1089. default:
  1090. break;
  1091. case NFS4_OPEN_CLAIM_NULL:
  1092. case NFS4_OPEN_CLAIM_FH:
  1093. p->o_arg.access = NFS4_ACCESS_READ |
  1094. NFS4_ACCESS_MODIFY |
  1095. NFS4_ACCESS_EXTEND |
  1096. NFS4_ACCESS_EXECUTE;
  1097. }
  1098. }
  1099. p->o_arg.clientid = server->nfs_client->cl_clientid;
  1100. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  1101. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  1102. p->o_arg.name = &dentry->d_name;
  1103. p->o_arg.server = server;
  1104. p->o_arg.bitmask = nfs4_bitmask(server, label);
  1105. p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
  1106. p->o_arg.label = nfs4_label_copy(p->a_label, label);
  1107. switch (p->o_arg.claim) {
  1108. case NFS4_OPEN_CLAIM_NULL:
  1109. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1110. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  1111. p->o_arg.fh = NFS_FH(dir);
  1112. break;
  1113. case NFS4_OPEN_CLAIM_PREVIOUS:
  1114. case NFS4_OPEN_CLAIM_FH:
  1115. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1116. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1117. p->o_arg.fh = NFS_FH(d_inode(dentry));
  1118. }
  1119. if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
  1120. p->o_arg.u.attrs = &p->attrs;
  1121. memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
  1122. memcpy(p->o_arg.u.verifier.data, c->verf,
  1123. sizeof(p->o_arg.u.verifier.data));
  1124. }
  1125. p->c_arg.fh = &p->o_res.fh;
  1126. p->c_arg.stateid = &p->o_res.stateid;
  1127. p->c_arg.seqid = p->o_arg.seqid;
  1128. nfs4_init_opendata_res(p);
  1129. kref_init(&p->kref);
  1130. return p;
  1131. err_free_label:
  1132. nfs4_label_free(p->a_label);
  1133. err_free_f:
  1134. nfs4_label_free(p->f_label);
  1135. err_free_p:
  1136. kfree(p);
  1137. err:
  1138. dput(parent);
  1139. return NULL;
  1140. }
  1141. static void nfs4_opendata_free(struct kref *kref)
  1142. {
  1143. struct nfs4_opendata *p = container_of(kref,
  1144. struct nfs4_opendata, kref);
  1145. struct super_block *sb = p->dentry->d_sb;
  1146. nfs4_lgopen_release(p->lgp);
  1147. nfs_free_seqid(p->o_arg.seqid);
  1148. nfs4_sequence_free_slot(&p->o_res.seq_res);
  1149. if (p->state != NULL)
  1150. nfs4_put_open_state(p->state);
  1151. nfs4_put_state_owner(p->owner);
  1152. nfs4_label_free(p->a_label);
  1153. nfs4_label_free(p->f_label);
  1154. dput(p->dir);
  1155. dput(p->dentry);
  1156. nfs_sb_deactive(sb);
  1157. nfs_fattr_free_names(&p->f_attr);
  1158. kfree(p->f_attr.mdsthreshold);
  1159. kfree(p);
  1160. }
  1161. static void nfs4_opendata_put(struct nfs4_opendata *p)
  1162. {
  1163. if (p != NULL)
  1164. kref_put(&p->kref, nfs4_opendata_free);
  1165. }
  1166. static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
  1167. fmode_t fmode)
  1168. {
  1169. switch(fmode & (FMODE_READ|FMODE_WRITE)) {
  1170. case FMODE_READ|FMODE_WRITE:
  1171. return state->n_rdwr != 0;
  1172. case FMODE_WRITE:
  1173. return state->n_wronly != 0;
  1174. case FMODE_READ:
  1175. return state->n_rdonly != 0;
  1176. }
  1177. WARN_ON_ONCE(1);
  1178. return false;
  1179. }
  1180. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  1181. {
  1182. int ret = 0;
  1183. if (open_mode & (O_EXCL|O_TRUNC))
  1184. goto out;
  1185. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  1186. case FMODE_READ:
  1187. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  1188. && state->n_rdonly != 0;
  1189. break;
  1190. case FMODE_WRITE:
  1191. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  1192. && state->n_wronly != 0;
  1193. break;
  1194. case FMODE_READ|FMODE_WRITE:
  1195. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  1196. && state->n_rdwr != 0;
  1197. }
  1198. out:
  1199. return ret;
  1200. }
  1201. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
  1202. enum open_claim_type4 claim)
  1203. {
  1204. if (delegation == NULL)
  1205. return 0;
  1206. if ((delegation->type & fmode) != fmode)
  1207. return 0;
  1208. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  1209. return 0;
  1210. switch (claim) {
  1211. case NFS4_OPEN_CLAIM_NULL:
  1212. case NFS4_OPEN_CLAIM_FH:
  1213. break;
  1214. case NFS4_OPEN_CLAIM_PREVIOUS:
  1215. if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  1216. break;
  1217. default:
  1218. return 0;
  1219. }
  1220. nfs_mark_delegation_referenced(delegation);
  1221. return 1;
  1222. }
  1223. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  1224. {
  1225. switch (fmode) {
  1226. case FMODE_WRITE:
  1227. state->n_wronly++;
  1228. break;
  1229. case FMODE_READ:
  1230. state->n_rdonly++;
  1231. break;
  1232. case FMODE_READ|FMODE_WRITE:
  1233. state->n_rdwr++;
  1234. }
  1235. nfs4_state_set_mode_locked(state, state->state | fmode);
  1236. }
  1237. #ifdef CONFIG_NFS_V4_1
  1238. static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
  1239. {
  1240. if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
  1241. return true;
  1242. if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
  1243. return true;
  1244. if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
  1245. return true;
  1246. return false;
  1247. }
  1248. #endif /* CONFIG_NFS_V4_1 */
  1249. static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
  1250. {
  1251. if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
  1252. wake_up_all(&state->waitq);
  1253. }
  1254. static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
  1255. const nfs4_stateid *stateid)
  1256. {
  1257. u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
  1258. u32 stateid_seqid = be32_to_cpu(stateid->seqid);
  1259. if (stateid_seqid == state_seqid + 1U ||
  1260. (stateid_seqid == 1U && state_seqid == 0xffffffffU))
  1261. nfs_state_log_update_open_stateid(state);
  1262. else
  1263. set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
  1264. }
  1265. static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
  1266. {
  1267. struct nfs_client *clp = state->owner->so_server->nfs_client;
  1268. bool need_recover = false;
  1269. if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
  1270. need_recover = true;
  1271. if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
  1272. need_recover = true;
  1273. if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
  1274. need_recover = true;
  1275. if (need_recover)
  1276. nfs4_state_mark_reclaim_nograce(clp, state);
  1277. }
  1278. /*
  1279. * Check for whether or not the caller may update the open stateid
  1280. * to the value passed in by stateid.
  1281. *
  1282. * Note: This function relies heavily on the server implementing
  1283. * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
  1284. * correctly.
  1285. * i.e. The stateid seqids have to be initialised to 1, and
  1286. * are then incremented on every state transition.
  1287. */
  1288. static bool nfs_need_update_open_stateid(struct nfs4_state *state,
  1289. const nfs4_stateid *stateid)
  1290. {
  1291. if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
  1292. !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
  1293. if (stateid->seqid == cpu_to_be32(1))
  1294. nfs_state_log_update_open_stateid(state);
  1295. else
  1296. set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
  1297. return true;
  1298. }
  1299. if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
  1300. nfs_state_log_out_of_order_open_stateid(state, stateid);
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
  1306. {
  1307. if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
  1308. return;
  1309. if (state->n_wronly)
  1310. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  1311. if (state->n_rdonly)
  1312. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  1313. if (state->n_rdwr)
  1314. set_bit(NFS_O_RDWR_STATE, &state->flags);
  1315. set_bit(NFS_OPEN_STATE, &state->flags);
  1316. }
  1317. static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
  1318. nfs4_stateid *stateid, fmode_t fmode)
  1319. {
  1320. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1321. switch (fmode & (FMODE_READ|FMODE_WRITE)) {
  1322. case FMODE_WRITE:
  1323. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1324. break;
  1325. case FMODE_READ:
  1326. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1327. break;
  1328. case 0:
  1329. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1330. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1331. clear_bit(NFS_OPEN_STATE, &state->flags);
  1332. }
  1333. if (stateid == NULL)
  1334. return;
  1335. /* Handle OPEN+OPEN_DOWNGRADE races */
  1336. if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
  1337. !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
  1338. nfs_resync_open_stateid_locked(state);
  1339. goto out;
  1340. }
  1341. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1342. nfs4_stateid_copy(&state->stateid, stateid);
  1343. nfs4_stateid_copy(&state->open_stateid, stateid);
  1344. trace_nfs4_open_stateid_update(state->inode, stateid, 0);
  1345. out:
  1346. nfs_state_log_update_open_stateid(state);
  1347. }
  1348. static void nfs_clear_open_stateid(struct nfs4_state *state,
  1349. nfs4_stateid *arg_stateid,
  1350. nfs4_stateid *stateid, fmode_t fmode)
  1351. {
  1352. write_seqlock(&state->seqlock);
  1353. /* Ignore, if the CLOSE argment doesn't match the current stateid */
  1354. if (nfs4_state_match_open_stateid_other(state, arg_stateid))
  1355. nfs_clear_open_stateid_locked(state, stateid, fmode);
  1356. write_sequnlock(&state->seqlock);
  1357. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
  1358. nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
  1359. }
  1360. static void nfs_set_open_stateid_locked(struct nfs4_state *state,
  1361. const nfs4_stateid *stateid, nfs4_stateid *freeme)
  1362. {
  1363. DEFINE_WAIT(wait);
  1364. int status = 0;
  1365. for (;;) {
  1366. if (!nfs_need_update_open_stateid(state, stateid))
  1367. return;
  1368. if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
  1369. break;
  1370. if (status)
  1371. break;
  1372. /* Rely on seqids for serialisation with NFSv4.0 */
  1373. if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
  1374. break;
  1375. prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
  1376. /*
  1377. * Ensure we process the state changes in the same order
  1378. * in which the server processed them by delaying the
  1379. * update of the stateid until we are in sequence.
  1380. */
  1381. write_sequnlock(&state->seqlock);
  1382. spin_unlock(&state->owner->so_lock);
  1383. rcu_read_unlock();
  1384. trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
  1385. if (!signal_pending(current)) {
  1386. if (schedule_timeout(5*HZ) == 0)
  1387. status = -EAGAIN;
  1388. else
  1389. status = 0;
  1390. } else
  1391. status = -EINTR;
  1392. finish_wait(&state->waitq, &wait);
  1393. rcu_read_lock();
  1394. spin_lock(&state->owner->so_lock);
  1395. write_seqlock(&state->seqlock);
  1396. }
  1397. if (test_bit(NFS_OPEN_STATE, &state->flags) &&
  1398. !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
  1399. nfs4_stateid_copy(freeme, &state->open_stateid);
  1400. nfs_test_and_clear_all_open_stateid(state);
  1401. }
  1402. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1403. nfs4_stateid_copy(&state->stateid, stateid);
  1404. nfs4_stateid_copy(&state->open_stateid, stateid);
  1405. trace_nfs4_open_stateid_update(state->inode, stateid, status);
  1406. nfs_state_log_update_open_stateid(state);
  1407. }
  1408. static void nfs_state_set_open_stateid(struct nfs4_state *state,
  1409. const nfs4_stateid *open_stateid,
  1410. fmode_t fmode,
  1411. nfs4_stateid *freeme)
  1412. {
  1413. /*
  1414. * Protect the call to nfs4_state_set_mode_locked and
  1415. * serialise the stateid update
  1416. */
  1417. write_seqlock(&state->seqlock);
  1418. nfs_set_open_stateid_locked(state, open_stateid, freeme);
  1419. switch (fmode) {
  1420. case FMODE_READ:
  1421. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  1422. break;
  1423. case FMODE_WRITE:
  1424. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  1425. break;
  1426. case FMODE_READ|FMODE_WRITE:
  1427. set_bit(NFS_O_RDWR_STATE, &state->flags);
  1428. }
  1429. set_bit(NFS_OPEN_STATE, &state->flags);
  1430. write_sequnlock(&state->seqlock);
  1431. }
  1432. static void nfs_state_set_delegation(struct nfs4_state *state,
  1433. const nfs4_stateid *deleg_stateid,
  1434. fmode_t fmode)
  1435. {
  1436. /*
  1437. * Protect the call to nfs4_state_set_mode_locked and
  1438. * serialise the stateid update
  1439. */
  1440. write_seqlock(&state->seqlock);
  1441. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  1442. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1443. write_sequnlock(&state->seqlock);
  1444. }
  1445. static int update_open_stateid(struct nfs4_state *state,
  1446. const nfs4_stateid *open_stateid,
  1447. const nfs4_stateid *delegation,
  1448. fmode_t fmode)
  1449. {
  1450. struct nfs_server *server = NFS_SERVER(state->inode);
  1451. struct nfs_client *clp = server->nfs_client;
  1452. struct nfs_inode *nfsi = NFS_I(state->inode);
  1453. struct nfs_delegation *deleg_cur;
  1454. nfs4_stateid freeme = { };
  1455. int ret = 0;
  1456. fmode &= (FMODE_READ|FMODE_WRITE);
  1457. rcu_read_lock();
  1458. spin_lock(&state->owner->so_lock);
  1459. if (open_stateid != NULL) {
  1460. nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
  1461. ret = 1;
  1462. }
  1463. deleg_cur = rcu_dereference(nfsi->delegation);
  1464. if (deleg_cur == NULL)
  1465. goto no_delegation;
  1466. spin_lock(&deleg_cur->lock);
  1467. if (rcu_dereference(nfsi->delegation) != deleg_cur ||
  1468. test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
  1469. (deleg_cur->type & fmode) != fmode)
  1470. goto no_delegation_unlock;
  1471. if (delegation == NULL)
  1472. delegation = &deleg_cur->stateid;
  1473. else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
  1474. goto no_delegation_unlock;
  1475. nfs_mark_delegation_referenced(deleg_cur);
  1476. nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
  1477. ret = 1;
  1478. no_delegation_unlock:
  1479. spin_unlock(&deleg_cur->lock);
  1480. no_delegation:
  1481. if (ret)
  1482. update_open_stateflags(state, fmode);
  1483. spin_unlock(&state->owner->so_lock);
  1484. rcu_read_unlock();
  1485. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
  1486. nfs4_schedule_state_manager(clp);
  1487. if (freeme.type != 0)
  1488. nfs4_test_and_free_stateid(server, &freeme,
  1489. state->owner->so_cred);
  1490. return ret;
  1491. }
  1492. static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
  1493. const nfs4_stateid *stateid)
  1494. {
  1495. struct nfs4_state *state = lsp->ls_state;
  1496. bool ret = false;
  1497. spin_lock(&state->state_lock);
  1498. if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
  1499. goto out_noupdate;
  1500. if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
  1501. goto out_noupdate;
  1502. nfs4_stateid_copy(&lsp->ls_stateid, stateid);
  1503. ret = true;
  1504. out_noupdate:
  1505. spin_unlock(&state->state_lock);
  1506. return ret;
  1507. }
  1508. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  1509. {
  1510. struct nfs_delegation *delegation;
  1511. fmode &= FMODE_READ|FMODE_WRITE;
  1512. rcu_read_lock();
  1513. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1514. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  1515. rcu_read_unlock();
  1516. return;
  1517. }
  1518. rcu_read_unlock();
  1519. nfs4_inode_return_delegation(inode);
  1520. }
  1521. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  1522. {
  1523. struct nfs4_state *state = opendata->state;
  1524. struct nfs_inode *nfsi = NFS_I(state->inode);
  1525. struct nfs_delegation *delegation;
  1526. int open_mode = opendata->o_arg.open_flags;
  1527. fmode_t fmode = opendata->o_arg.fmode;
  1528. enum open_claim_type4 claim = opendata->o_arg.claim;
  1529. nfs4_stateid stateid;
  1530. int ret = -EAGAIN;
  1531. for (;;) {
  1532. spin_lock(&state->owner->so_lock);
  1533. if (can_open_cached(state, fmode, open_mode)) {
  1534. update_open_stateflags(state, fmode);
  1535. spin_unlock(&state->owner->so_lock);
  1536. goto out_return_state;
  1537. }
  1538. spin_unlock(&state->owner->so_lock);
  1539. rcu_read_lock();
  1540. delegation = rcu_dereference(nfsi->delegation);
  1541. if (!can_open_delegated(delegation, fmode, claim)) {
  1542. rcu_read_unlock();
  1543. break;
  1544. }
  1545. /* Save the delegation */
  1546. nfs4_stateid_copy(&stateid, &delegation->stateid);
  1547. rcu_read_unlock();
  1548. nfs_release_seqid(opendata->o_arg.seqid);
  1549. if (!opendata->is_recover) {
  1550. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  1551. if (ret != 0)
  1552. goto out;
  1553. }
  1554. ret = -EAGAIN;
  1555. /* Try to update the stateid using the delegation */
  1556. if (update_open_stateid(state, NULL, &stateid, fmode))
  1557. goto out_return_state;
  1558. }
  1559. out:
  1560. return ERR_PTR(ret);
  1561. out_return_state:
  1562. atomic_inc(&state->count);
  1563. return state;
  1564. }
  1565. static void
  1566. nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
  1567. {
  1568. struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
  1569. struct nfs_delegation *delegation;
  1570. int delegation_flags = 0;
  1571. rcu_read_lock();
  1572. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1573. if (delegation)
  1574. delegation_flags = delegation->flags;
  1575. rcu_read_unlock();
  1576. switch (data->o_arg.claim) {
  1577. default:
  1578. break;
  1579. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1580. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1581. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  1582. "returning a delegation for "
  1583. "OPEN(CLAIM_DELEGATE_CUR)\n",
  1584. clp->cl_hostname);
  1585. return;
  1586. }
  1587. if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  1588. nfs_inode_set_delegation(state->inode,
  1589. data->owner->so_cred,
  1590. data->o_res.delegation_type,
  1591. &data->o_res.delegation,
  1592. data->o_res.pagemod_limit);
  1593. else
  1594. nfs_inode_reclaim_delegation(state->inode,
  1595. data->owner->so_cred,
  1596. data->o_res.delegation_type,
  1597. &data->o_res.delegation,
  1598. data->o_res.pagemod_limit);
  1599. }
  1600. /*
  1601. * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
  1602. * and update the nfs4_state.
  1603. */
  1604. static struct nfs4_state *
  1605. _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
  1606. {
  1607. struct inode *inode = data->state->inode;
  1608. struct nfs4_state *state = data->state;
  1609. int ret;
  1610. if (!data->rpc_done) {
  1611. if (data->rpc_status)
  1612. return ERR_PTR(data->rpc_status);
  1613. /* cached opens have already been processed */
  1614. goto update;
  1615. }
  1616. ret = nfs_refresh_inode(inode, &data->f_attr);
  1617. if (ret)
  1618. return ERR_PTR(ret);
  1619. if (data->o_res.delegation_type != 0)
  1620. nfs4_opendata_check_deleg(data, state);
  1621. update:
  1622. update_open_stateid(state, &data->o_res.stateid, NULL,
  1623. data->o_arg.fmode);
  1624. atomic_inc(&state->count);
  1625. return state;
  1626. }
  1627. static struct inode *
  1628. nfs4_opendata_get_inode(struct nfs4_opendata *data)
  1629. {
  1630. struct inode *inode;
  1631. switch (data->o_arg.claim) {
  1632. case NFS4_OPEN_CLAIM_NULL:
  1633. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1634. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  1635. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  1636. return ERR_PTR(-EAGAIN);
  1637. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
  1638. &data->f_attr, data->f_label);
  1639. break;
  1640. default:
  1641. inode = d_inode(data->dentry);
  1642. ihold(inode);
  1643. nfs_refresh_inode(inode, &data->f_attr);
  1644. }
  1645. return inode;
  1646. }
  1647. static struct nfs4_state *
  1648. nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
  1649. {
  1650. struct nfs4_state *state;
  1651. struct inode *inode;
  1652. inode = nfs4_opendata_get_inode(data);
  1653. if (IS_ERR(inode))
  1654. return ERR_CAST(inode);
  1655. if (data->state != NULL && data->state->inode == inode) {
  1656. state = data->state;
  1657. atomic_inc(&state->count);
  1658. } else
  1659. state = nfs4_get_open_state(inode, data->owner);
  1660. iput(inode);
  1661. if (state == NULL)
  1662. state = ERR_PTR(-ENOMEM);
  1663. return state;
  1664. }
  1665. static struct nfs4_state *
  1666. _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1667. {
  1668. struct nfs4_state *state;
  1669. if (!data->rpc_done) {
  1670. state = nfs4_try_open_cached(data);
  1671. trace_nfs4_cached_open(data->state);
  1672. goto out;
  1673. }
  1674. state = nfs4_opendata_find_nfs4_state(data);
  1675. if (IS_ERR(state))
  1676. goto out;
  1677. if (data->o_res.delegation_type != 0)
  1678. nfs4_opendata_check_deleg(data, state);
  1679. update_open_stateid(state, &data->o_res.stateid, NULL,
  1680. data->o_arg.fmode);
  1681. out:
  1682. nfs_release_seqid(data->o_arg.seqid);
  1683. return state;
  1684. }
  1685. static struct nfs4_state *
  1686. nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1687. {
  1688. struct nfs4_state *ret;
  1689. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
  1690. ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
  1691. else
  1692. ret = _nfs4_opendata_to_nfs4_state(data);
  1693. nfs4_sequence_free_slot(&data->o_res.seq_res);
  1694. return ret;
  1695. }
  1696. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  1697. {
  1698. struct nfs_inode *nfsi = NFS_I(state->inode);
  1699. struct nfs_open_context *ctx;
  1700. spin_lock(&state->inode->i_lock);
  1701. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1702. if (ctx->state != state)
  1703. continue;
  1704. get_nfs_open_context(ctx);
  1705. spin_unlock(&state->inode->i_lock);
  1706. return ctx;
  1707. }
  1708. spin_unlock(&state->inode->i_lock);
  1709. return ERR_PTR(-ENOENT);
  1710. }
  1711. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
  1712. struct nfs4_state *state, enum open_claim_type4 claim)
  1713. {
  1714. struct nfs4_opendata *opendata;
  1715. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
  1716. NULL, claim, GFP_NOFS);
  1717. if (opendata == NULL)
  1718. return ERR_PTR(-ENOMEM);
  1719. opendata->state = state;
  1720. atomic_inc(&state->count);
  1721. return opendata;
  1722. }
  1723. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
  1724. fmode_t fmode)
  1725. {
  1726. struct nfs4_state *newstate;
  1727. int ret;
  1728. if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
  1729. return 0;
  1730. opendata->o_arg.open_flags = 0;
  1731. opendata->o_arg.fmode = fmode;
  1732. opendata->o_arg.share_access = nfs4_map_atomic_open_share(
  1733. NFS_SB(opendata->dentry->d_sb),
  1734. fmode, 0);
  1735. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1736. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1737. nfs4_init_opendata_res(opendata);
  1738. ret = _nfs4_recover_proc_open(opendata);
  1739. if (ret != 0)
  1740. return ret;
  1741. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1742. if (IS_ERR(newstate))
  1743. return PTR_ERR(newstate);
  1744. if (newstate != opendata->state)
  1745. ret = -ESTALE;
  1746. nfs4_close_state(newstate, fmode);
  1747. return ret;
  1748. }
  1749. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1750. {
  1751. int ret;
  1752. /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
  1753. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1754. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1755. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1756. /* memory barrier prior to reading state->n_* */
  1757. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1758. clear_bit(NFS_OPEN_STATE, &state->flags);
  1759. smp_rmb();
  1760. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
  1761. if (ret != 0)
  1762. return ret;
  1763. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
  1764. if (ret != 0)
  1765. return ret;
  1766. ret = nfs4_open_recover_helper(opendata, FMODE_READ);
  1767. if (ret != 0)
  1768. return ret;
  1769. /*
  1770. * We may have performed cached opens for all three recoveries.
  1771. * Check if we need to update the current stateid.
  1772. */
  1773. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1774. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1775. write_seqlock(&state->seqlock);
  1776. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1777. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1778. write_sequnlock(&state->seqlock);
  1779. }
  1780. return 0;
  1781. }
  1782. /*
  1783. * OPEN_RECLAIM:
  1784. * reclaim state on the server after a reboot.
  1785. */
  1786. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1787. {
  1788. struct nfs_delegation *delegation;
  1789. struct nfs4_opendata *opendata;
  1790. fmode_t delegation_type = 0;
  1791. int status;
  1792. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1793. NFS4_OPEN_CLAIM_PREVIOUS);
  1794. if (IS_ERR(opendata))
  1795. return PTR_ERR(opendata);
  1796. rcu_read_lock();
  1797. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1798. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1799. delegation_type = delegation->type;
  1800. rcu_read_unlock();
  1801. opendata->o_arg.u.delegation_type = delegation_type;
  1802. status = nfs4_open_recover(opendata, state);
  1803. nfs4_opendata_put(opendata);
  1804. return status;
  1805. }
  1806. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1807. {
  1808. struct nfs_server *server = NFS_SERVER(state->inode);
  1809. struct nfs4_exception exception = { };
  1810. int err;
  1811. do {
  1812. err = _nfs4_do_open_reclaim(ctx, state);
  1813. trace_nfs4_open_reclaim(ctx, 0, err);
  1814. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  1815. continue;
  1816. if (err != -NFS4ERR_DELAY)
  1817. break;
  1818. nfs4_handle_exception(server, err, &exception);
  1819. } while (exception.retry);
  1820. return err;
  1821. }
  1822. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1823. {
  1824. struct nfs_open_context *ctx;
  1825. int ret;
  1826. ctx = nfs4_state_find_open_context(state);
  1827. if (IS_ERR(ctx))
  1828. return -EAGAIN;
  1829. ret = nfs4_do_open_reclaim(ctx, state);
  1830. put_nfs_open_context(ctx);
  1831. return ret;
  1832. }
  1833. static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
  1834. {
  1835. switch (err) {
  1836. default:
  1837. printk(KERN_ERR "NFS: %s: unhandled error "
  1838. "%d.\n", __func__, err);
  1839. case 0:
  1840. case -ENOENT:
  1841. case -EAGAIN:
  1842. case -ESTALE:
  1843. break;
  1844. case -NFS4ERR_BADSESSION:
  1845. case -NFS4ERR_BADSLOT:
  1846. case -NFS4ERR_BAD_HIGH_SLOT:
  1847. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1848. case -NFS4ERR_DEADSESSION:
  1849. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1850. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  1851. return -EAGAIN;
  1852. case -NFS4ERR_STALE_CLIENTID:
  1853. case -NFS4ERR_STALE_STATEID:
  1854. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1855. /* Don't recall a delegation if it was lost */
  1856. nfs4_schedule_lease_recovery(server->nfs_client);
  1857. return -EAGAIN;
  1858. case -NFS4ERR_MOVED:
  1859. nfs4_schedule_migration_recovery(server);
  1860. return -EAGAIN;
  1861. case -NFS4ERR_LEASE_MOVED:
  1862. nfs4_schedule_lease_moved_recovery(server->nfs_client);
  1863. return -EAGAIN;
  1864. case -NFS4ERR_DELEG_REVOKED:
  1865. case -NFS4ERR_ADMIN_REVOKED:
  1866. case -NFS4ERR_EXPIRED:
  1867. case -NFS4ERR_BAD_STATEID:
  1868. case -NFS4ERR_OPENMODE:
  1869. nfs_inode_find_state_and_recover(state->inode,
  1870. stateid);
  1871. nfs4_schedule_stateid_recovery(server, state);
  1872. return -EAGAIN;
  1873. case -NFS4ERR_DELAY:
  1874. case -NFS4ERR_GRACE:
  1875. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1876. ssleep(1);
  1877. return -EAGAIN;
  1878. case -ENOMEM:
  1879. case -NFS4ERR_DENIED:
  1880. if (fl) {
  1881. struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
  1882. if (lsp)
  1883. set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
  1884. }
  1885. return 0;
  1886. }
  1887. return err;
  1888. }
  1889. int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
  1890. struct nfs4_state *state, const nfs4_stateid *stateid,
  1891. fmode_t type)
  1892. {
  1893. struct nfs_server *server = NFS_SERVER(state->inode);
  1894. struct nfs4_opendata *opendata;
  1895. int err = 0;
  1896. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1897. NFS4_OPEN_CLAIM_DELEG_CUR_FH);
  1898. if (IS_ERR(opendata))
  1899. return PTR_ERR(opendata);
  1900. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  1901. write_seqlock(&state->seqlock);
  1902. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1903. write_sequnlock(&state->seqlock);
  1904. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1905. switch (type & (FMODE_READ|FMODE_WRITE)) {
  1906. case FMODE_READ|FMODE_WRITE:
  1907. case FMODE_WRITE:
  1908. err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
  1909. if (err)
  1910. break;
  1911. err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
  1912. if (err)
  1913. break;
  1914. case FMODE_READ:
  1915. err = nfs4_open_recover_helper(opendata, FMODE_READ);
  1916. }
  1917. nfs4_opendata_put(opendata);
  1918. return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
  1919. }
  1920. static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
  1921. {
  1922. struct nfs4_opendata *data = calldata;
  1923. nfs4_setup_sequence(data->o_arg.server->nfs_client,
  1924. &data->c_arg.seq_args, &data->c_res.seq_res, task);
  1925. }
  1926. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1927. {
  1928. struct nfs4_opendata *data = calldata;
  1929. nfs40_sequence_done(task, &data->c_res.seq_res);
  1930. data->rpc_status = task->tk_status;
  1931. if (data->rpc_status == 0) {
  1932. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  1933. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1934. renew_lease(data->o_res.server, data->timestamp);
  1935. data->rpc_done = true;
  1936. }
  1937. }
  1938. static void nfs4_open_confirm_release(void *calldata)
  1939. {
  1940. struct nfs4_opendata *data = calldata;
  1941. struct nfs4_state *state = NULL;
  1942. /* If this request hasn't been cancelled, do nothing */
  1943. if (!data->cancelled)
  1944. goto out_free;
  1945. /* In case of error, no cleanup! */
  1946. if (!data->rpc_done)
  1947. goto out_free;
  1948. state = nfs4_opendata_to_nfs4_state(data);
  1949. if (!IS_ERR(state))
  1950. nfs4_close_state(state, data->o_arg.fmode);
  1951. out_free:
  1952. nfs4_opendata_put(data);
  1953. }
  1954. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1955. .rpc_call_prepare = nfs4_open_confirm_prepare,
  1956. .rpc_call_done = nfs4_open_confirm_done,
  1957. .rpc_release = nfs4_open_confirm_release,
  1958. };
  1959. /*
  1960. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1961. */
  1962. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1963. {
  1964. struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
  1965. struct rpc_task *task;
  1966. struct rpc_message msg = {
  1967. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1968. .rpc_argp = &data->c_arg,
  1969. .rpc_resp = &data->c_res,
  1970. .rpc_cred = data->owner->so_cred,
  1971. };
  1972. struct rpc_task_setup task_setup_data = {
  1973. .rpc_client = server->client,
  1974. .rpc_message = &msg,
  1975. .callback_ops = &nfs4_open_confirm_ops,
  1976. .callback_data = data,
  1977. .workqueue = nfsiod_workqueue,
  1978. .flags = RPC_TASK_ASYNC,
  1979. };
  1980. int status;
  1981. nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
  1982. data->is_recover);
  1983. kref_get(&data->kref);
  1984. data->rpc_done = false;
  1985. data->rpc_status = 0;
  1986. data->timestamp = jiffies;
  1987. task = rpc_run_task(&task_setup_data);
  1988. if (IS_ERR(task))
  1989. return PTR_ERR(task);
  1990. status = rpc_wait_for_completion_task(task);
  1991. if (status != 0) {
  1992. data->cancelled = true;
  1993. smp_wmb();
  1994. } else
  1995. status = data->rpc_status;
  1996. rpc_put_task(task);
  1997. return status;
  1998. }
  1999. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  2000. {
  2001. struct nfs4_opendata *data = calldata;
  2002. struct nfs4_state_owner *sp = data->owner;
  2003. struct nfs_client *clp = sp->so_server->nfs_client;
  2004. enum open_claim_type4 claim = data->o_arg.claim;
  2005. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  2006. goto out_wait;
  2007. /*
  2008. * Check if we still need to send an OPEN call, or if we can use
  2009. * a delegation instead.
  2010. */
  2011. if (data->state != NULL) {
  2012. struct nfs_delegation *delegation;
  2013. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  2014. goto out_no_action;
  2015. rcu_read_lock();
  2016. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  2017. if (can_open_delegated(delegation, data->o_arg.fmode, claim))
  2018. goto unlock_no_action;
  2019. rcu_read_unlock();
  2020. }
  2021. /* Update client id. */
  2022. data->o_arg.clientid = clp->cl_clientid;
  2023. switch (claim) {
  2024. default:
  2025. break;
  2026. case NFS4_OPEN_CLAIM_PREVIOUS:
  2027. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  2028. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  2029. data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
  2030. case NFS4_OPEN_CLAIM_FH:
  2031. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  2032. }
  2033. data->timestamp = jiffies;
  2034. if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
  2035. &data->o_arg.seq_args,
  2036. &data->o_res.seq_res,
  2037. task) != 0)
  2038. nfs_release_seqid(data->o_arg.seqid);
  2039. /* Set the create mode (note dependency on the session type) */
  2040. data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
  2041. if (data->o_arg.open_flags & O_EXCL) {
  2042. data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
  2043. if (nfs4_has_persistent_session(clp))
  2044. data->o_arg.createmode = NFS4_CREATE_GUARDED;
  2045. else if (clp->cl_mvops->minor_version > 0)
  2046. data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
  2047. }
  2048. return;
  2049. unlock_no_action:
  2050. trace_nfs4_cached_open(data->state);
  2051. rcu_read_unlock();
  2052. out_no_action:
  2053. task->tk_action = NULL;
  2054. out_wait:
  2055. nfs4_sequence_done(task, &data->o_res.seq_res);
  2056. }
  2057. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  2058. {
  2059. struct nfs4_opendata *data = calldata;
  2060. data->rpc_status = task->tk_status;
  2061. if (!nfs4_sequence_process(task, &data->o_res.seq_res))
  2062. return;
  2063. if (task->tk_status == 0) {
  2064. if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
  2065. switch (data->o_res.f_attr->mode & S_IFMT) {
  2066. case S_IFREG:
  2067. break;
  2068. case S_IFLNK:
  2069. data->rpc_status = -ELOOP;
  2070. break;
  2071. case S_IFDIR:
  2072. data->rpc_status = -EISDIR;
  2073. break;
  2074. default:
  2075. data->rpc_status = -ENOTDIR;
  2076. }
  2077. }
  2078. renew_lease(data->o_res.server, data->timestamp);
  2079. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  2080. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  2081. }
  2082. data->rpc_done = true;
  2083. }
  2084. static void nfs4_open_release(void *calldata)
  2085. {
  2086. struct nfs4_opendata *data = calldata;
  2087. struct nfs4_state *state = NULL;
  2088. /* If this request hasn't been cancelled, do nothing */
  2089. if (!data->cancelled)
  2090. goto out_free;
  2091. /* In case of error, no cleanup! */
  2092. if (data->rpc_status != 0 || !data->rpc_done)
  2093. goto out_free;
  2094. /* In case we need an open_confirm, no cleanup! */
  2095. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  2096. goto out_free;
  2097. state = nfs4_opendata_to_nfs4_state(data);
  2098. if (!IS_ERR(state))
  2099. nfs4_close_state(state, data->o_arg.fmode);
  2100. out_free:
  2101. nfs4_opendata_put(data);
  2102. }
  2103. static const struct rpc_call_ops nfs4_open_ops = {
  2104. .rpc_call_prepare = nfs4_open_prepare,
  2105. .rpc_call_done = nfs4_open_done,
  2106. .rpc_release = nfs4_open_release,
  2107. };
  2108. static int nfs4_run_open_task(struct nfs4_opendata *data,
  2109. struct nfs_open_context *ctx)
  2110. {
  2111. struct inode *dir = d_inode(data->dir);
  2112. struct nfs_server *server = NFS_SERVER(dir);
  2113. struct nfs_openargs *o_arg = &data->o_arg;
  2114. struct nfs_openres *o_res = &data->o_res;
  2115. struct rpc_task *task;
  2116. struct rpc_message msg = {
  2117. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  2118. .rpc_argp = o_arg,
  2119. .rpc_resp = o_res,
  2120. .rpc_cred = data->owner->so_cred,
  2121. };
  2122. struct rpc_task_setup task_setup_data = {
  2123. .rpc_client = server->client,
  2124. .rpc_message = &msg,
  2125. .callback_ops = &nfs4_open_ops,
  2126. .callback_data = data,
  2127. .workqueue = nfsiod_workqueue,
  2128. .flags = RPC_TASK_ASYNC,
  2129. };
  2130. int status;
  2131. kref_get(&data->kref);
  2132. data->rpc_done = false;
  2133. data->rpc_status = 0;
  2134. data->cancelled = false;
  2135. data->is_recover = false;
  2136. if (!ctx) {
  2137. nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
  2138. data->is_recover = true;
  2139. } else {
  2140. nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
  2141. pnfs_lgopen_prepare(data, ctx);
  2142. }
  2143. task = rpc_run_task(&task_setup_data);
  2144. if (IS_ERR(task))
  2145. return PTR_ERR(task);
  2146. status = rpc_wait_for_completion_task(task);
  2147. if (status != 0) {
  2148. data->cancelled = true;
  2149. smp_wmb();
  2150. } else
  2151. status = data->rpc_status;
  2152. rpc_put_task(task);
  2153. return status;
  2154. }
  2155. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  2156. {
  2157. struct inode *dir = d_inode(data->dir);
  2158. struct nfs_openres *o_res = &data->o_res;
  2159. int status;
  2160. status = nfs4_run_open_task(data, NULL);
  2161. if (status != 0 || !data->rpc_done)
  2162. return status;
  2163. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  2164. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
  2165. status = _nfs4_proc_open_confirm(data);
  2166. return status;
  2167. }
  2168. /*
  2169. * Additional permission checks in order to distinguish between an
  2170. * open for read, and an open for execute. This works around the
  2171. * fact that NFSv4 OPEN treats read and execute permissions as being
  2172. * the same.
  2173. * Note that in the non-execute case, we want to turn off permission
  2174. * checking if we just created a new file (POSIX open() semantics).
  2175. */
  2176. static int nfs4_opendata_access(struct rpc_cred *cred,
  2177. struct nfs4_opendata *opendata,
  2178. struct nfs4_state *state, fmode_t fmode,
  2179. int openflags)
  2180. {
  2181. struct nfs_access_entry cache;
  2182. u32 mask, flags;
  2183. /* access call failed or for some reason the server doesn't
  2184. * support any access modes -- defer access call until later */
  2185. if (opendata->o_res.access_supported == 0)
  2186. return 0;
  2187. mask = 0;
  2188. /*
  2189. * Use openflags to check for exec, because fmode won't
  2190. * always have FMODE_EXEC set when file open for exec.
  2191. */
  2192. if (openflags & __FMODE_EXEC) {
  2193. /* ONLY check for exec rights */
  2194. if (S_ISDIR(state->inode->i_mode))
  2195. mask = NFS4_ACCESS_LOOKUP;
  2196. else
  2197. mask = NFS4_ACCESS_EXECUTE;
  2198. } else if ((fmode & FMODE_READ) && !opendata->file_created)
  2199. mask = NFS4_ACCESS_READ;
  2200. cache.cred = cred;
  2201. nfs_access_set_mask(&cache, opendata->o_res.access_result);
  2202. nfs_access_add_cache(state->inode, &cache);
  2203. flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
  2204. if ((mask & ~cache.mask & flags) == 0)
  2205. return 0;
  2206. return -EACCES;
  2207. }
  2208. /*
  2209. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  2210. */
  2211. static int _nfs4_proc_open(struct nfs4_opendata *data,
  2212. struct nfs_open_context *ctx)
  2213. {
  2214. struct inode *dir = d_inode(data->dir);
  2215. struct nfs_server *server = NFS_SERVER(dir);
  2216. struct nfs_openargs *o_arg = &data->o_arg;
  2217. struct nfs_openres *o_res = &data->o_res;
  2218. int status;
  2219. status = nfs4_run_open_task(data, ctx);
  2220. if (!data->rpc_done)
  2221. return status;
  2222. if (status != 0) {
  2223. if (status == -NFS4ERR_BADNAME &&
  2224. !(o_arg->open_flags & O_CREAT))
  2225. return -ENOENT;
  2226. return status;
  2227. }
  2228. nfs_fattr_map_and_free_names(server, &data->f_attr);
  2229. if (o_arg->open_flags & O_CREAT) {
  2230. if (o_arg->open_flags & O_EXCL)
  2231. data->file_created = true;
  2232. else if (o_res->cinfo.before != o_res->cinfo.after)
  2233. data->file_created = true;
  2234. if (data->file_created ||
  2235. inode_peek_iversion_raw(dir) != o_res->cinfo.after)
  2236. update_changeattr(dir, &o_res->cinfo,
  2237. o_res->f_attr->time_start);
  2238. }
  2239. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  2240. server->caps &= ~NFS_CAP_POSIX_LOCK;
  2241. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  2242. status = _nfs4_proc_open_confirm(data);
  2243. if (status != 0)
  2244. return status;
  2245. }
  2246. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
  2247. nfs4_sequence_free_slot(&o_res->seq_res);
  2248. nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
  2249. o_res->f_label, NULL);
  2250. }
  2251. return 0;
  2252. }
  2253. /*
  2254. * OPEN_EXPIRED:
  2255. * reclaim state on the server after a network partition.
  2256. * Assumes caller holds the appropriate lock
  2257. */
  2258. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  2259. {
  2260. struct nfs4_opendata *opendata;
  2261. int ret;
  2262. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  2263. NFS4_OPEN_CLAIM_FH);
  2264. if (IS_ERR(opendata))
  2265. return PTR_ERR(opendata);
  2266. ret = nfs4_open_recover(opendata, state);
  2267. if (ret == -ESTALE)
  2268. d_drop(ctx->dentry);
  2269. nfs4_opendata_put(opendata);
  2270. return ret;
  2271. }
  2272. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  2273. {
  2274. struct nfs_server *server = NFS_SERVER(state->inode);
  2275. struct nfs4_exception exception = { };
  2276. int err;
  2277. do {
  2278. err = _nfs4_open_expired(ctx, state);
  2279. trace_nfs4_open_expired(ctx, 0, err);
  2280. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  2281. continue;
  2282. switch (err) {
  2283. default:
  2284. goto out;
  2285. case -NFS4ERR_GRACE:
  2286. case -NFS4ERR_DELAY:
  2287. nfs4_handle_exception(server, err, &exception);
  2288. err = 0;
  2289. }
  2290. } while (exception.retry);
  2291. out:
  2292. return err;
  2293. }
  2294. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2295. {
  2296. struct nfs_open_context *ctx;
  2297. int ret;
  2298. ctx = nfs4_state_find_open_context(state);
  2299. if (IS_ERR(ctx))
  2300. return -EAGAIN;
  2301. ret = nfs4_do_open_expired(ctx, state);
  2302. put_nfs_open_context(ctx);
  2303. return ret;
  2304. }
  2305. static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
  2306. const nfs4_stateid *stateid)
  2307. {
  2308. nfs_remove_bad_delegation(state->inode, stateid);
  2309. write_seqlock(&state->seqlock);
  2310. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  2311. write_sequnlock(&state->seqlock);
  2312. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  2313. }
  2314. static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
  2315. {
  2316. if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
  2317. nfs_finish_clear_delegation_stateid(state, NULL);
  2318. }
  2319. static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2320. {
  2321. /* NFSv4.0 doesn't allow for delegation recovery on open expire */
  2322. nfs40_clear_delegation_stateid(state);
  2323. return nfs4_open_expired(sp, state);
  2324. }
  2325. static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
  2326. nfs4_stateid *stateid,
  2327. struct rpc_cred *cred)
  2328. {
  2329. return -NFS4ERR_BAD_STATEID;
  2330. }
  2331. #if defined(CONFIG_NFS_V4_1)
  2332. static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
  2333. nfs4_stateid *stateid,
  2334. struct rpc_cred *cred)
  2335. {
  2336. int status;
  2337. switch (stateid->type) {
  2338. default:
  2339. break;
  2340. case NFS4_INVALID_STATEID_TYPE:
  2341. case NFS4_SPECIAL_STATEID_TYPE:
  2342. return -NFS4ERR_BAD_STATEID;
  2343. case NFS4_REVOKED_STATEID_TYPE:
  2344. goto out_free;
  2345. }
  2346. status = nfs41_test_stateid(server, stateid, cred);
  2347. switch (status) {
  2348. case -NFS4ERR_EXPIRED:
  2349. case -NFS4ERR_ADMIN_REVOKED:
  2350. case -NFS4ERR_DELEG_REVOKED:
  2351. break;
  2352. default:
  2353. return status;
  2354. }
  2355. out_free:
  2356. /* Ack the revoked state to the server */
  2357. nfs41_free_stateid(server, stateid, cred, true);
  2358. return -NFS4ERR_EXPIRED;
  2359. }
  2360. static void nfs41_check_delegation_stateid(struct nfs4_state *state)
  2361. {
  2362. struct nfs_server *server = NFS_SERVER(state->inode);
  2363. nfs4_stateid stateid;
  2364. struct nfs_delegation *delegation;
  2365. struct rpc_cred *cred;
  2366. int status;
  2367. /* Get the delegation credential for use by test/free_stateid */
  2368. rcu_read_lock();
  2369. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  2370. if (delegation == NULL) {
  2371. rcu_read_unlock();
  2372. return;
  2373. }
  2374. nfs4_stateid_copy(&stateid, &delegation->stateid);
  2375. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
  2376. !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
  2377. &delegation->flags)) {
  2378. rcu_read_unlock();
  2379. nfs_finish_clear_delegation_stateid(state, &stateid);
  2380. return;
  2381. }
  2382. cred = get_rpccred(delegation->cred);
  2383. rcu_read_unlock();
  2384. status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
  2385. trace_nfs4_test_delegation_stateid(state, NULL, status);
  2386. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
  2387. nfs_finish_clear_delegation_stateid(state, &stateid);
  2388. put_rpccred(cred);
  2389. }
  2390. /**
  2391. * nfs41_check_expired_locks - possibly free a lock stateid
  2392. *
  2393. * @state: NFSv4 state for an inode
  2394. *
  2395. * Returns NFS_OK if recovery for this stateid is now finished.
  2396. * Otherwise a negative NFS4ERR value is returned.
  2397. */
  2398. static int nfs41_check_expired_locks(struct nfs4_state *state)
  2399. {
  2400. int status, ret = NFS_OK;
  2401. struct nfs4_lock_state *lsp, *prev = NULL;
  2402. struct nfs_server *server = NFS_SERVER(state->inode);
  2403. if (!test_bit(LK_STATE_IN_USE, &state->flags))
  2404. goto out;
  2405. spin_lock(&state->state_lock);
  2406. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  2407. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  2408. struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
  2409. refcount_inc(&lsp->ls_count);
  2410. spin_unlock(&state->state_lock);
  2411. nfs4_put_lock_state(prev);
  2412. prev = lsp;
  2413. status = nfs41_test_and_free_expired_stateid(server,
  2414. &lsp->ls_stateid,
  2415. cred);
  2416. trace_nfs4_test_lock_stateid(state, lsp, status);
  2417. if (status == -NFS4ERR_EXPIRED ||
  2418. status == -NFS4ERR_BAD_STATEID) {
  2419. clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  2420. lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
  2421. if (!recover_lost_locks)
  2422. set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
  2423. } else if (status != NFS_OK) {
  2424. ret = status;
  2425. nfs4_put_lock_state(prev);
  2426. goto out;
  2427. }
  2428. spin_lock(&state->state_lock);
  2429. }
  2430. }
  2431. spin_unlock(&state->state_lock);
  2432. nfs4_put_lock_state(prev);
  2433. out:
  2434. return ret;
  2435. }
  2436. /**
  2437. * nfs41_check_open_stateid - possibly free an open stateid
  2438. *
  2439. * @state: NFSv4 state for an inode
  2440. *
  2441. * Returns NFS_OK if recovery for this stateid is now finished.
  2442. * Otherwise a negative NFS4ERR value is returned.
  2443. */
  2444. static int nfs41_check_open_stateid(struct nfs4_state *state)
  2445. {
  2446. struct nfs_server *server = NFS_SERVER(state->inode);
  2447. nfs4_stateid *stateid = &state->open_stateid;
  2448. struct rpc_cred *cred = state->owner->so_cred;
  2449. int status;
  2450. if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
  2451. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
  2452. if (nfs4_have_delegation(state->inode, state->state))
  2453. return NFS_OK;
  2454. return -NFS4ERR_OPENMODE;
  2455. }
  2456. return -NFS4ERR_BAD_STATEID;
  2457. }
  2458. status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
  2459. trace_nfs4_test_open_stateid(state, NULL, status);
  2460. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
  2461. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  2462. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  2463. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  2464. clear_bit(NFS_OPEN_STATE, &state->flags);
  2465. stateid->type = NFS4_INVALID_STATEID_TYPE;
  2466. return status;
  2467. }
  2468. if (nfs_open_stateid_recover_openmode(state))
  2469. return -NFS4ERR_OPENMODE;
  2470. return NFS_OK;
  2471. }
  2472. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2473. {
  2474. int status;
  2475. nfs41_check_delegation_stateid(state);
  2476. status = nfs41_check_expired_locks(state);
  2477. if (status != NFS_OK)
  2478. return status;
  2479. status = nfs41_check_open_stateid(state);
  2480. if (status != NFS_OK)
  2481. status = nfs4_open_expired(sp, state);
  2482. return status;
  2483. }
  2484. #endif
  2485. /*
  2486. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  2487. * fields corresponding to attributes that were used to store the verifier.
  2488. * Make sure we clobber those fields in the later setattr call
  2489. */
  2490. static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
  2491. struct iattr *sattr, struct nfs4_label **label)
  2492. {
  2493. const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
  2494. __u32 attrset[3];
  2495. unsigned ret;
  2496. unsigned i;
  2497. for (i = 0; i < ARRAY_SIZE(attrset); i++) {
  2498. attrset[i] = opendata->o_res.attrset[i];
  2499. if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
  2500. attrset[i] &= ~bitmask[i];
  2501. }
  2502. ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
  2503. sattr->ia_valid : 0;
  2504. if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
  2505. if (sattr->ia_valid & ATTR_ATIME_SET)
  2506. ret |= ATTR_ATIME_SET;
  2507. else
  2508. ret |= ATTR_ATIME;
  2509. }
  2510. if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
  2511. if (sattr->ia_valid & ATTR_MTIME_SET)
  2512. ret |= ATTR_MTIME_SET;
  2513. else
  2514. ret |= ATTR_MTIME;
  2515. }
  2516. if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
  2517. *label = NULL;
  2518. return ret;
  2519. }
  2520. static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  2521. fmode_t fmode,
  2522. int flags,
  2523. struct nfs_open_context *ctx)
  2524. {
  2525. struct nfs4_state_owner *sp = opendata->owner;
  2526. struct nfs_server *server = sp->so_server;
  2527. struct dentry *dentry;
  2528. struct nfs4_state *state;
  2529. unsigned int seq;
  2530. int ret;
  2531. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  2532. ret = _nfs4_proc_open(opendata, ctx);
  2533. if (ret != 0)
  2534. goto out;
  2535. state = _nfs4_opendata_to_nfs4_state(opendata);
  2536. ret = PTR_ERR(state);
  2537. if (IS_ERR(state))
  2538. goto out;
  2539. ctx->state = state;
  2540. if (server->caps & NFS_CAP_POSIX_LOCK)
  2541. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  2542. if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
  2543. set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
  2544. dentry = opendata->dentry;
  2545. if (d_really_is_negative(dentry)) {
  2546. struct dentry *alias;
  2547. d_drop(dentry);
  2548. alias = d_exact_alias(dentry, state->inode);
  2549. if (!alias)
  2550. alias = d_splice_alias(igrab(state->inode), dentry);
  2551. /* d_splice_alias() can't fail here - it's a non-directory */
  2552. if (alias) {
  2553. dput(ctx->dentry);
  2554. ctx->dentry = dentry = alias;
  2555. }
  2556. nfs_set_verifier(dentry,
  2557. nfs_save_change_attribute(d_inode(opendata->dir)));
  2558. }
  2559. ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
  2560. if (ret != 0)
  2561. goto out;
  2562. if (d_inode(dentry) == state->inode) {
  2563. nfs_inode_attach_open_context(ctx);
  2564. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  2565. nfs4_schedule_stateid_recovery(server, state);
  2566. else
  2567. pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
  2568. }
  2569. out:
  2570. nfs4_sequence_free_slot(&opendata->o_res.seq_res);
  2571. return ret;
  2572. }
  2573. /*
  2574. * Returns a referenced nfs4_state
  2575. */
  2576. static int _nfs4_do_open(struct inode *dir,
  2577. struct nfs_open_context *ctx,
  2578. int flags,
  2579. const struct nfs4_open_createattrs *c,
  2580. int *opened)
  2581. {
  2582. struct nfs4_state_owner *sp;
  2583. struct nfs4_state *state = NULL;
  2584. struct nfs_server *server = NFS_SERVER(dir);
  2585. struct nfs4_opendata *opendata;
  2586. struct dentry *dentry = ctx->dentry;
  2587. struct rpc_cred *cred = ctx->cred;
  2588. struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
  2589. fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
  2590. enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
  2591. struct iattr *sattr = c->sattr;
  2592. struct nfs4_label *label = c->label;
  2593. struct nfs4_label *olabel = NULL;
  2594. int status;
  2595. /* Protect against reboot recovery conflicts */
  2596. status = -ENOMEM;
  2597. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  2598. if (sp == NULL) {
  2599. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  2600. goto out_err;
  2601. }
  2602. status = nfs4_client_recover_expired_lease(server->nfs_client);
  2603. if (status != 0)
  2604. goto err_put_state_owner;
  2605. if (d_really_is_positive(dentry))
  2606. nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
  2607. status = -ENOMEM;
  2608. if (d_really_is_positive(dentry))
  2609. claim = NFS4_OPEN_CLAIM_FH;
  2610. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
  2611. c, claim, GFP_KERNEL);
  2612. if (opendata == NULL)
  2613. goto err_put_state_owner;
  2614. if (label) {
  2615. olabel = nfs4_label_alloc(server, GFP_KERNEL);
  2616. if (IS_ERR(olabel)) {
  2617. status = PTR_ERR(olabel);
  2618. goto err_opendata_put;
  2619. }
  2620. }
  2621. if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  2622. if (!opendata->f_attr.mdsthreshold) {
  2623. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  2624. if (!opendata->f_attr.mdsthreshold)
  2625. goto err_free_label;
  2626. }
  2627. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  2628. }
  2629. if (d_really_is_positive(dentry))
  2630. opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
  2631. status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
  2632. if (status != 0)
  2633. goto err_free_label;
  2634. state = ctx->state;
  2635. if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
  2636. (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
  2637. unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
  2638. /*
  2639. * send create attributes which was not set by open
  2640. * with an extra setattr.
  2641. */
  2642. if (attrs || label) {
  2643. unsigned ia_old = sattr->ia_valid;
  2644. sattr->ia_valid = attrs;
  2645. nfs_fattr_init(opendata->o_res.f_attr);
  2646. status = nfs4_do_setattr(state->inode, cred,
  2647. opendata->o_res.f_attr, sattr,
  2648. ctx, label, olabel);
  2649. if (status == 0) {
  2650. nfs_setattr_update_inode(state->inode, sattr,
  2651. opendata->o_res.f_attr);
  2652. nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
  2653. }
  2654. sattr->ia_valid = ia_old;
  2655. }
  2656. }
  2657. if (opened && opendata->file_created)
  2658. *opened |= FILE_CREATED;
  2659. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
  2660. *ctx_th = opendata->f_attr.mdsthreshold;
  2661. opendata->f_attr.mdsthreshold = NULL;
  2662. }
  2663. nfs4_label_free(olabel);
  2664. nfs4_opendata_put(opendata);
  2665. nfs4_put_state_owner(sp);
  2666. return 0;
  2667. err_free_label:
  2668. nfs4_label_free(olabel);
  2669. err_opendata_put:
  2670. nfs4_opendata_put(opendata);
  2671. err_put_state_owner:
  2672. nfs4_put_state_owner(sp);
  2673. out_err:
  2674. return status;
  2675. }
  2676. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  2677. struct nfs_open_context *ctx,
  2678. int flags,
  2679. struct iattr *sattr,
  2680. struct nfs4_label *label,
  2681. int *opened)
  2682. {
  2683. struct nfs_server *server = NFS_SERVER(dir);
  2684. struct nfs4_exception exception = { };
  2685. struct nfs4_state *res;
  2686. struct nfs4_open_createattrs c = {
  2687. .label = label,
  2688. .sattr = sattr,
  2689. .verf = {
  2690. [0] = (__u32)jiffies,
  2691. [1] = (__u32)current->pid,
  2692. },
  2693. };
  2694. int status;
  2695. do {
  2696. status = _nfs4_do_open(dir, ctx, flags, &c, opened);
  2697. res = ctx->state;
  2698. trace_nfs4_open_file(ctx, flags, status);
  2699. if (status == 0)
  2700. break;
  2701. /* NOTE: BAD_SEQID means the server and client disagree about the
  2702. * book-keeping w.r.t. state-changing operations
  2703. * (OPEN/CLOSE/LOCK/LOCKU...)
  2704. * It is actually a sign of a bug on the client or on the server.
  2705. *
  2706. * If we receive a BAD_SEQID error in the particular case of
  2707. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  2708. * have unhashed the old state_owner for us, and that we can
  2709. * therefore safely retry using a new one. We should still warn
  2710. * the user though...
  2711. */
  2712. if (status == -NFS4ERR_BAD_SEQID) {
  2713. pr_warn_ratelimited("NFS: v4 server %s "
  2714. " returned a bad sequence-id error!\n",
  2715. NFS_SERVER(dir)->nfs_client->cl_hostname);
  2716. exception.retry = 1;
  2717. continue;
  2718. }
  2719. /*
  2720. * BAD_STATEID on OPEN means that the server cancelled our
  2721. * state before it received the OPEN_CONFIRM.
  2722. * Recover by retrying the request as per the discussion
  2723. * on Page 181 of RFC3530.
  2724. */
  2725. if (status == -NFS4ERR_BAD_STATEID) {
  2726. exception.retry = 1;
  2727. continue;
  2728. }
  2729. if (status == -EAGAIN) {
  2730. /* We must have found a delegation */
  2731. exception.retry = 1;
  2732. continue;
  2733. }
  2734. if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
  2735. continue;
  2736. res = ERR_PTR(nfs4_handle_exception(server,
  2737. status, &exception));
  2738. } while (exception.retry);
  2739. return res;
  2740. }
  2741. static int _nfs4_do_setattr(struct inode *inode,
  2742. struct nfs_setattrargs *arg,
  2743. struct nfs_setattrres *res,
  2744. struct rpc_cred *cred,
  2745. struct nfs_open_context *ctx)
  2746. {
  2747. struct nfs_server *server = NFS_SERVER(inode);
  2748. struct rpc_message msg = {
  2749. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  2750. .rpc_argp = arg,
  2751. .rpc_resp = res,
  2752. .rpc_cred = cred,
  2753. };
  2754. struct rpc_cred *delegation_cred = NULL;
  2755. unsigned long timestamp = jiffies;
  2756. bool truncate;
  2757. int status;
  2758. nfs_fattr_init(res->fattr);
  2759. /* Servers should only apply open mode checks for file size changes */
  2760. truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
  2761. if (!truncate)
  2762. goto zero_stateid;
  2763. if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
  2764. /* Use that stateid */
  2765. } else if (ctx != NULL) {
  2766. struct nfs_lock_context *l_ctx;
  2767. if (!nfs4_valid_open_stateid(ctx->state))
  2768. return -EBADF;
  2769. l_ctx = nfs_get_lock_context(ctx);
  2770. if (IS_ERR(l_ctx))
  2771. return PTR_ERR(l_ctx);
  2772. status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
  2773. &arg->stateid, &delegation_cred);
  2774. nfs_put_lock_context(l_ctx);
  2775. if (status == -EIO)
  2776. return -EBADF;
  2777. } else {
  2778. zero_stateid:
  2779. nfs4_stateid_copy(&arg->stateid, &zero_stateid);
  2780. }
  2781. if (delegation_cred)
  2782. msg.rpc_cred = delegation_cred;
  2783. status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
  2784. put_rpccred(delegation_cred);
  2785. if (status == 0 && ctx != NULL)
  2786. renew_lease(server, timestamp);
  2787. trace_nfs4_setattr(inode, &arg->stateid, status);
  2788. return status;
  2789. }
  2790. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  2791. struct nfs_fattr *fattr, struct iattr *sattr,
  2792. struct nfs_open_context *ctx, struct nfs4_label *ilabel,
  2793. struct nfs4_label *olabel)
  2794. {
  2795. struct nfs_server *server = NFS_SERVER(inode);
  2796. __u32 bitmask[NFS4_BITMASK_SZ];
  2797. struct nfs4_state *state = ctx ? ctx->state : NULL;
  2798. struct nfs_setattrargs arg = {
  2799. .fh = NFS_FH(inode),
  2800. .iap = sattr,
  2801. .server = server,
  2802. .bitmask = bitmask,
  2803. .label = ilabel,
  2804. };
  2805. struct nfs_setattrres res = {
  2806. .fattr = fattr,
  2807. .label = olabel,
  2808. .server = server,
  2809. };
  2810. struct nfs4_exception exception = {
  2811. .state = state,
  2812. .inode = inode,
  2813. .stateid = &arg.stateid,
  2814. };
  2815. int err;
  2816. do {
  2817. nfs4_bitmap_copy_adjust_setattr(bitmask,
  2818. nfs4_bitmask(server, olabel),
  2819. inode);
  2820. err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
  2821. switch (err) {
  2822. case -NFS4ERR_OPENMODE:
  2823. if (!(sattr->ia_valid & ATTR_SIZE)) {
  2824. pr_warn_once("NFSv4: server %s is incorrectly "
  2825. "applying open mode checks to "
  2826. "a SETATTR that is not "
  2827. "changing file size.\n",
  2828. server->nfs_client->cl_hostname);
  2829. }
  2830. if (state && !(state->state & FMODE_WRITE)) {
  2831. err = -EBADF;
  2832. if (sattr->ia_valid & ATTR_OPEN)
  2833. err = -EACCES;
  2834. goto out;
  2835. }
  2836. }
  2837. err = nfs4_handle_exception(server, err, &exception);
  2838. } while (exception.retry);
  2839. out:
  2840. return err;
  2841. }
  2842. static bool
  2843. nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
  2844. {
  2845. if (inode == NULL || !nfs_have_layout(inode))
  2846. return false;
  2847. return pnfs_wait_on_layoutreturn(inode, task);
  2848. }
  2849. struct nfs4_closedata {
  2850. struct inode *inode;
  2851. struct nfs4_state *state;
  2852. struct nfs_closeargs arg;
  2853. struct nfs_closeres res;
  2854. struct {
  2855. struct nfs4_layoutreturn_args arg;
  2856. struct nfs4_layoutreturn_res res;
  2857. struct nfs4_xdr_opaque_data ld_private;
  2858. u32 roc_barrier;
  2859. bool roc;
  2860. } lr;
  2861. struct nfs_fattr fattr;
  2862. unsigned long timestamp;
  2863. };
  2864. static void nfs4_free_closedata(void *data)
  2865. {
  2866. struct nfs4_closedata *calldata = data;
  2867. struct nfs4_state_owner *sp = calldata->state->owner;
  2868. struct super_block *sb = calldata->state->inode->i_sb;
  2869. if (calldata->lr.roc)
  2870. pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
  2871. calldata->res.lr_ret);
  2872. nfs4_put_open_state(calldata->state);
  2873. nfs_free_seqid(calldata->arg.seqid);
  2874. nfs4_put_state_owner(sp);
  2875. nfs_sb_deactive(sb);
  2876. kfree(calldata);
  2877. }
  2878. static void nfs4_close_done(struct rpc_task *task, void *data)
  2879. {
  2880. struct nfs4_closedata *calldata = data;
  2881. struct nfs4_state *state = calldata->state;
  2882. struct nfs_server *server = NFS_SERVER(calldata->inode);
  2883. nfs4_stateid *res_stateid = NULL;
  2884. struct nfs4_exception exception = {
  2885. .state = state,
  2886. .inode = calldata->inode,
  2887. .stateid = &calldata->arg.stateid,
  2888. };
  2889. dprintk("%s: begin!\n", __func__);
  2890. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  2891. return;
  2892. trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
  2893. /* Handle Layoutreturn errors */
  2894. if (calldata->arg.lr_args && task->tk_status != 0) {
  2895. switch (calldata->res.lr_ret) {
  2896. default:
  2897. calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  2898. break;
  2899. case 0:
  2900. calldata->arg.lr_args = NULL;
  2901. calldata->res.lr_res = NULL;
  2902. break;
  2903. case -NFS4ERR_OLD_STATEID:
  2904. if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
  2905. calldata->inode))
  2906. goto lr_restart;
  2907. /* Fallthrough */
  2908. case -NFS4ERR_ADMIN_REVOKED:
  2909. case -NFS4ERR_DELEG_REVOKED:
  2910. case -NFS4ERR_EXPIRED:
  2911. case -NFS4ERR_BAD_STATEID:
  2912. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  2913. case -NFS4ERR_WRONG_CRED:
  2914. calldata->arg.lr_args = NULL;
  2915. calldata->res.lr_res = NULL;
  2916. goto lr_restart;
  2917. }
  2918. }
  2919. /* hmm. we are done with the inode, and in the process of freeing
  2920. * the state_owner. we keep this around to process errors
  2921. */
  2922. switch (task->tk_status) {
  2923. case 0:
  2924. res_stateid = &calldata->res.stateid;
  2925. renew_lease(server, calldata->timestamp);
  2926. break;
  2927. case -NFS4ERR_ACCESS:
  2928. if (calldata->arg.bitmask != NULL) {
  2929. calldata->arg.bitmask = NULL;
  2930. calldata->res.fattr = NULL;
  2931. goto out_restart;
  2932. }
  2933. break;
  2934. case -NFS4ERR_OLD_STATEID:
  2935. /* Did we race with OPEN? */
  2936. if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
  2937. state))
  2938. goto out_restart;
  2939. goto out_release;
  2940. case -NFS4ERR_ADMIN_REVOKED:
  2941. case -NFS4ERR_STALE_STATEID:
  2942. case -NFS4ERR_EXPIRED:
  2943. nfs4_free_revoked_stateid(server,
  2944. &calldata->arg.stateid,
  2945. task->tk_msg.rpc_cred);
  2946. /* Fallthrough */
  2947. case -NFS4ERR_BAD_STATEID:
  2948. break;
  2949. default:
  2950. task->tk_status = nfs4_async_handle_exception(task,
  2951. server, task->tk_status, &exception);
  2952. if (exception.retry)
  2953. goto out_restart;
  2954. }
  2955. nfs_clear_open_stateid(state, &calldata->arg.stateid,
  2956. res_stateid, calldata->arg.fmode);
  2957. out_release:
  2958. task->tk_status = 0;
  2959. nfs_release_seqid(calldata->arg.seqid);
  2960. nfs_refresh_inode(calldata->inode, &calldata->fattr);
  2961. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  2962. return;
  2963. lr_restart:
  2964. calldata->res.lr_ret = 0;
  2965. out_restart:
  2966. task->tk_status = 0;
  2967. rpc_restart_call_prepare(task);
  2968. goto out_release;
  2969. }
  2970. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  2971. {
  2972. struct nfs4_closedata *calldata = data;
  2973. struct nfs4_state *state = calldata->state;
  2974. struct inode *inode = calldata->inode;
  2975. bool is_rdonly, is_wronly, is_rdwr;
  2976. int call_close = 0;
  2977. dprintk("%s: begin!\n", __func__);
  2978. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  2979. goto out_wait;
  2980. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  2981. spin_lock(&state->owner->so_lock);
  2982. is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
  2983. is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
  2984. is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
  2985. /* Calculate the change in open mode */
  2986. calldata->arg.fmode = 0;
  2987. if (state->n_rdwr == 0) {
  2988. if (state->n_rdonly == 0)
  2989. call_close |= is_rdonly;
  2990. else if (is_rdonly)
  2991. calldata->arg.fmode |= FMODE_READ;
  2992. if (state->n_wronly == 0)
  2993. call_close |= is_wronly;
  2994. else if (is_wronly)
  2995. calldata->arg.fmode |= FMODE_WRITE;
  2996. if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
  2997. call_close |= is_rdwr;
  2998. } else if (is_rdwr)
  2999. calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
  3000. if (!nfs4_valid_open_stateid(state) ||
  3001. !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
  3002. call_close = 0;
  3003. spin_unlock(&state->owner->so_lock);
  3004. if (!call_close) {
  3005. /* Note: exit _without_ calling nfs4_close_done */
  3006. goto out_no_action;
  3007. }
  3008. if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
  3009. nfs_release_seqid(calldata->arg.seqid);
  3010. goto out_wait;
  3011. }
  3012. if (calldata->arg.fmode == 0)
  3013. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  3014. if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
  3015. /* Close-to-open cache consistency revalidation */
  3016. if (!nfs4_have_delegation(inode, FMODE_READ))
  3017. calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  3018. else
  3019. calldata->arg.bitmask = NULL;
  3020. }
  3021. calldata->arg.share_access =
  3022. nfs4_map_atomic_open_share(NFS_SERVER(inode),
  3023. calldata->arg.fmode, 0);
  3024. if (calldata->res.fattr == NULL)
  3025. calldata->arg.bitmask = NULL;
  3026. else if (calldata->arg.bitmask == NULL)
  3027. calldata->res.fattr = NULL;
  3028. calldata->timestamp = jiffies;
  3029. if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
  3030. &calldata->arg.seq_args,
  3031. &calldata->res.seq_res,
  3032. task) != 0)
  3033. nfs_release_seqid(calldata->arg.seqid);
  3034. dprintk("%s: done!\n", __func__);
  3035. return;
  3036. out_no_action:
  3037. task->tk_action = NULL;
  3038. out_wait:
  3039. nfs4_sequence_done(task, &calldata->res.seq_res);
  3040. }
  3041. static const struct rpc_call_ops nfs4_close_ops = {
  3042. .rpc_call_prepare = nfs4_close_prepare,
  3043. .rpc_call_done = nfs4_close_done,
  3044. .rpc_release = nfs4_free_closedata,
  3045. };
  3046. /*
  3047. * It is possible for data to be read/written from a mem-mapped file
  3048. * after the sys_close call (which hits the vfs layer as a flush).
  3049. * This means that we can't safely call nfsv4 close on a file until
  3050. * the inode is cleared. This in turn means that we are not good
  3051. * NFSv4 citizens - we do not indicate to the server to update the file's
  3052. * share state even when we are done with one of the three share
  3053. * stateid's in the inode.
  3054. *
  3055. * NOTE: Caller must be holding the sp->so_owner semaphore!
  3056. */
  3057. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  3058. {
  3059. struct nfs_server *server = NFS_SERVER(state->inode);
  3060. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  3061. struct nfs4_closedata *calldata;
  3062. struct nfs4_state_owner *sp = state->owner;
  3063. struct rpc_task *task;
  3064. struct rpc_message msg = {
  3065. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  3066. .rpc_cred = state->owner->so_cred,
  3067. };
  3068. struct rpc_task_setup task_setup_data = {
  3069. .rpc_client = server->client,
  3070. .rpc_message = &msg,
  3071. .callback_ops = &nfs4_close_ops,
  3072. .workqueue = nfsiod_workqueue,
  3073. .flags = RPC_TASK_ASYNC,
  3074. };
  3075. int status = -ENOMEM;
  3076. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
  3077. &task_setup_data.rpc_client, &msg);
  3078. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  3079. if (calldata == NULL)
  3080. goto out;
  3081. nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
  3082. calldata->inode = state->inode;
  3083. calldata->state = state;
  3084. calldata->arg.fh = NFS_FH(state->inode);
  3085. if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
  3086. goto out_free_calldata;
  3087. /* Serialization for the sequence id */
  3088. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  3089. calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
  3090. if (IS_ERR(calldata->arg.seqid))
  3091. goto out_free_calldata;
  3092. nfs_fattr_init(&calldata->fattr);
  3093. calldata->arg.fmode = 0;
  3094. calldata->lr.arg.ld_private = &calldata->lr.ld_private;
  3095. calldata->res.fattr = &calldata->fattr;
  3096. calldata->res.seqid = calldata->arg.seqid;
  3097. calldata->res.server = server;
  3098. calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  3099. calldata->lr.roc = pnfs_roc(state->inode,
  3100. &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
  3101. if (calldata->lr.roc) {
  3102. calldata->arg.lr_args = &calldata->lr.arg;
  3103. calldata->res.lr_res = &calldata->lr.res;
  3104. }
  3105. nfs_sb_active(calldata->inode->i_sb);
  3106. msg.rpc_argp = &calldata->arg;
  3107. msg.rpc_resp = &calldata->res;
  3108. task_setup_data.callback_data = calldata;
  3109. task = rpc_run_task(&task_setup_data);
  3110. if (IS_ERR(task))
  3111. return PTR_ERR(task);
  3112. status = 0;
  3113. if (wait)
  3114. status = rpc_wait_for_completion_task(task);
  3115. rpc_put_task(task);
  3116. return status;
  3117. out_free_calldata:
  3118. kfree(calldata);
  3119. out:
  3120. nfs4_put_open_state(state);
  3121. nfs4_put_state_owner(sp);
  3122. return status;
  3123. }
  3124. static struct inode *
  3125. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
  3126. int open_flags, struct iattr *attr, int *opened)
  3127. {
  3128. struct nfs4_state *state;
  3129. struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
  3130. label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
  3131. /* Protect against concurrent sillydeletes */
  3132. state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
  3133. nfs4_label_release_security(label);
  3134. if (IS_ERR(state))
  3135. return ERR_CAST(state);
  3136. return state->inode;
  3137. }
  3138. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  3139. {
  3140. if (ctx->state == NULL)
  3141. return;
  3142. if (is_sync)
  3143. nfs4_close_sync(ctx->state, ctx->mode);
  3144. else
  3145. nfs4_close_state(ctx->state, ctx->mode);
  3146. }
  3147. #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
  3148. #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
  3149. #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
  3150. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  3151. {
  3152. u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
  3153. struct nfs4_server_caps_arg args = {
  3154. .fhandle = fhandle,
  3155. .bitmask = bitmask,
  3156. };
  3157. struct nfs4_server_caps_res res = {};
  3158. struct rpc_message msg = {
  3159. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  3160. .rpc_argp = &args,
  3161. .rpc_resp = &res,
  3162. };
  3163. int status;
  3164. int i;
  3165. bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
  3166. FATTR4_WORD0_FH_EXPIRE_TYPE |
  3167. FATTR4_WORD0_LINK_SUPPORT |
  3168. FATTR4_WORD0_SYMLINK_SUPPORT |
  3169. FATTR4_WORD0_ACLSUPPORT;
  3170. if (minorversion)
  3171. bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
  3172. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3173. if (status == 0) {
  3174. /* Sanity check the server answers */
  3175. switch (minorversion) {
  3176. case 0:
  3177. res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
  3178. res.attr_bitmask[2] = 0;
  3179. break;
  3180. case 1:
  3181. res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
  3182. break;
  3183. case 2:
  3184. res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
  3185. }
  3186. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  3187. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  3188. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  3189. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  3190. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  3191. NFS_CAP_CTIME|NFS_CAP_MTIME|
  3192. NFS_CAP_SECURITY_LABEL);
  3193. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
  3194. res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3195. server->caps |= NFS_CAP_ACLS;
  3196. if (res.has_links != 0)
  3197. server->caps |= NFS_CAP_HARDLINKS;
  3198. if (res.has_symlinks != 0)
  3199. server->caps |= NFS_CAP_SYMLINKS;
  3200. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  3201. server->caps |= NFS_CAP_FILEID;
  3202. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  3203. server->caps |= NFS_CAP_MODE;
  3204. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  3205. server->caps |= NFS_CAP_NLINK;
  3206. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  3207. server->caps |= NFS_CAP_OWNER;
  3208. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  3209. server->caps |= NFS_CAP_OWNER_GROUP;
  3210. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  3211. server->caps |= NFS_CAP_ATIME;
  3212. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  3213. server->caps |= NFS_CAP_CTIME;
  3214. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  3215. server->caps |= NFS_CAP_MTIME;
  3216. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  3217. if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
  3218. server->caps |= NFS_CAP_SECURITY_LABEL;
  3219. #endif
  3220. memcpy(server->attr_bitmask_nl, res.attr_bitmask,
  3221. sizeof(server->attr_bitmask));
  3222. server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  3223. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  3224. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  3225. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  3226. server->cache_consistency_bitmask[2] = 0;
  3227. /* Avoid a regression due to buggy server */
  3228. for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
  3229. res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
  3230. memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
  3231. sizeof(server->exclcreat_bitmask));
  3232. server->acl_bitmask = res.acl_bitmask;
  3233. server->fh_expire_type = res.fh_expire_type;
  3234. }
  3235. return status;
  3236. }
  3237. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  3238. {
  3239. struct nfs4_exception exception = { };
  3240. int err;
  3241. do {
  3242. err = nfs4_handle_exception(server,
  3243. _nfs4_server_capabilities(server, fhandle),
  3244. &exception);
  3245. } while (exception.retry);
  3246. return err;
  3247. }
  3248. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3249. struct nfs_fsinfo *info)
  3250. {
  3251. u32 bitmask[3];
  3252. struct nfs4_lookup_root_arg args = {
  3253. .bitmask = bitmask,
  3254. };
  3255. struct nfs4_lookup_res res = {
  3256. .server = server,
  3257. .fattr = info->fattr,
  3258. .fh = fhandle,
  3259. };
  3260. struct rpc_message msg = {
  3261. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  3262. .rpc_argp = &args,
  3263. .rpc_resp = &res,
  3264. };
  3265. bitmask[0] = nfs4_fattr_bitmap[0];
  3266. bitmask[1] = nfs4_fattr_bitmap[1];
  3267. /*
  3268. * Process the label in the upcoming getfattr
  3269. */
  3270. bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
  3271. nfs_fattr_init(info->fattr);
  3272. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3273. }
  3274. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3275. struct nfs_fsinfo *info)
  3276. {
  3277. struct nfs4_exception exception = { };
  3278. int err;
  3279. do {
  3280. err = _nfs4_lookup_root(server, fhandle, info);
  3281. trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
  3282. switch (err) {
  3283. case 0:
  3284. case -NFS4ERR_WRONGSEC:
  3285. goto out;
  3286. default:
  3287. err = nfs4_handle_exception(server, err, &exception);
  3288. }
  3289. } while (exception.retry);
  3290. out:
  3291. return err;
  3292. }
  3293. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3294. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  3295. {
  3296. struct rpc_auth_create_args auth_args = {
  3297. .pseudoflavor = flavor,
  3298. };
  3299. struct rpc_auth *auth;
  3300. auth = rpcauth_create(&auth_args, server->client);
  3301. if (IS_ERR(auth))
  3302. return -EACCES;
  3303. return nfs4_lookup_root(server, fhandle, info);
  3304. }
  3305. /*
  3306. * Retry pseudoroot lookup with various security flavors. We do this when:
  3307. *
  3308. * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
  3309. * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
  3310. *
  3311. * Returns zero on success, or a negative NFS4ERR value, or a
  3312. * negative errno value.
  3313. */
  3314. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3315. struct nfs_fsinfo *info)
  3316. {
  3317. /* Per 3530bis 15.33.5 */
  3318. static const rpc_authflavor_t flav_array[] = {
  3319. RPC_AUTH_GSS_KRB5P,
  3320. RPC_AUTH_GSS_KRB5I,
  3321. RPC_AUTH_GSS_KRB5,
  3322. RPC_AUTH_UNIX, /* courtesy */
  3323. RPC_AUTH_NULL,
  3324. };
  3325. int status = -EPERM;
  3326. size_t i;
  3327. if (server->auth_info.flavor_len > 0) {
  3328. /* try each flavor specified by user */
  3329. for (i = 0; i < server->auth_info.flavor_len; i++) {
  3330. status = nfs4_lookup_root_sec(server, fhandle, info,
  3331. server->auth_info.flavors[i]);
  3332. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3333. continue;
  3334. break;
  3335. }
  3336. } else {
  3337. /* no flavors specified by user, try default list */
  3338. for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
  3339. status = nfs4_lookup_root_sec(server, fhandle, info,
  3340. flav_array[i]);
  3341. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3342. continue;
  3343. break;
  3344. }
  3345. }
  3346. /*
  3347. * -EACCESS could mean that the user doesn't have correct permissions
  3348. * to access the mount. It could also mean that we tried to mount
  3349. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  3350. * existing mount programs don't handle -EACCES very well so it should
  3351. * be mapped to -EPERM instead.
  3352. */
  3353. if (status == -EACCES)
  3354. status = -EPERM;
  3355. return status;
  3356. }
  3357. /**
  3358. * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
  3359. * @server: initialized nfs_server handle
  3360. * @fhandle: we fill in the pseudo-fs root file handle
  3361. * @info: we fill in an FSINFO struct
  3362. * @auth_probe: probe the auth flavours
  3363. *
  3364. * Returns zero on success, or a negative errno.
  3365. */
  3366. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  3367. struct nfs_fsinfo *info,
  3368. bool auth_probe)
  3369. {
  3370. int status = 0;
  3371. if (!auth_probe)
  3372. status = nfs4_lookup_root(server, fhandle, info);
  3373. if (auth_probe || status == NFS4ERR_WRONGSEC)
  3374. status = server->nfs_client->cl_mvops->find_root_sec(server,
  3375. fhandle, info);
  3376. if (status == 0)
  3377. status = nfs4_server_capabilities(server, fhandle);
  3378. if (status == 0)
  3379. status = nfs4_do_fsinfo(server, fhandle, info);
  3380. return nfs4_map_errors(status);
  3381. }
  3382. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  3383. struct nfs_fsinfo *info)
  3384. {
  3385. int error;
  3386. struct nfs_fattr *fattr = info->fattr;
  3387. struct nfs4_label *label = NULL;
  3388. error = nfs4_server_capabilities(server, mntfh);
  3389. if (error < 0) {
  3390. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  3391. return error;
  3392. }
  3393. label = nfs4_label_alloc(server, GFP_KERNEL);
  3394. if (IS_ERR(label))
  3395. return PTR_ERR(label);
  3396. error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
  3397. if (error < 0) {
  3398. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  3399. goto err_free_label;
  3400. }
  3401. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  3402. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  3403. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  3404. err_free_label:
  3405. nfs4_label_free(label);
  3406. return error;
  3407. }
  3408. /*
  3409. * Get locations and (maybe) other attributes of a referral.
  3410. * Note that we'll actually follow the referral later when
  3411. * we detect fsid mismatch in inode revalidation
  3412. */
  3413. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  3414. const struct qstr *name, struct nfs_fattr *fattr,
  3415. struct nfs_fh *fhandle)
  3416. {
  3417. int status = -ENOMEM;
  3418. struct page *page = NULL;
  3419. struct nfs4_fs_locations *locations = NULL;
  3420. page = alloc_page(GFP_KERNEL);
  3421. if (page == NULL)
  3422. goto out;
  3423. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  3424. if (locations == NULL)
  3425. goto out;
  3426. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  3427. if (status != 0)
  3428. goto out;
  3429. /*
  3430. * If the fsid didn't change, this is a migration event, not a
  3431. * referral. Cause us to drop into the exception handler, which
  3432. * will kick off migration recovery.
  3433. */
  3434. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  3435. dprintk("%s: server did not return a different fsid for"
  3436. " a referral at %s\n", __func__, name->name);
  3437. status = -NFS4ERR_MOVED;
  3438. goto out;
  3439. }
  3440. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  3441. nfs_fixup_referral_attributes(&locations->fattr);
  3442. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  3443. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  3444. memset(fhandle, 0, sizeof(struct nfs_fh));
  3445. out:
  3446. if (page)
  3447. __free_page(page);
  3448. kfree(locations);
  3449. return status;
  3450. }
  3451. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3452. struct nfs_fattr *fattr, struct nfs4_label *label,
  3453. struct inode *inode)
  3454. {
  3455. __u32 bitmask[NFS4_BITMASK_SZ];
  3456. struct nfs4_getattr_arg args = {
  3457. .fh = fhandle,
  3458. .bitmask = bitmask,
  3459. };
  3460. struct nfs4_getattr_res res = {
  3461. .fattr = fattr,
  3462. .label = label,
  3463. .server = server,
  3464. };
  3465. struct rpc_message msg = {
  3466. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  3467. .rpc_argp = &args,
  3468. .rpc_resp = &res,
  3469. };
  3470. nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
  3471. nfs_fattr_init(fattr);
  3472. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3473. }
  3474. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3475. struct nfs_fattr *fattr, struct nfs4_label *label,
  3476. struct inode *inode)
  3477. {
  3478. struct nfs4_exception exception = { };
  3479. int err;
  3480. do {
  3481. err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
  3482. trace_nfs4_getattr(server, fhandle, fattr, err);
  3483. err = nfs4_handle_exception(server, err,
  3484. &exception);
  3485. } while (exception.retry);
  3486. return err;
  3487. }
  3488. /*
  3489. * The file is not closed if it is opened due to the a request to change
  3490. * the size of the file. The open call will not be needed once the
  3491. * VFS layer lookup-intents are implemented.
  3492. *
  3493. * Close is called when the inode is destroyed.
  3494. * If we haven't opened the file for O_WRONLY, we
  3495. * need to in the size_change case to obtain a stateid.
  3496. *
  3497. * Got race?
  3498. * Because OPEN is always done by name in nfsv4, it is
  3499. * possible that we opened a different file by the same
  3500. * name. We can recognize this race condition, but we
  3501. * can't do anything about it besides returning an error.
  3502. *
  3503. * This will be fixed with VFS changes (lookup-intent).
  3504. */
  3505. static int
  3506. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  3507. struct iattr *sattr)
  3508. {
  3509. struct inode *inode = d_inode(dentry);
  3510. struct rpc_cred *cred = NULL;
  3511. struct nfs_open_context *ctx = NULL;
  3512. struct nfs4_label *label = NULL;
  3513. int status;
  3514. if (pnfs_ld_layoutret_on_setattr(inode) &&
  3515. sattr->ia_valid & ATTR_SIZE &&
  3516. sattr->ia_size < i_size_read(inode))
  3517. pnfs_commit_and_return_layout(inode);
  3518. nfs_fattr_init(fattr);
  3519. /* Deal with open(O_TRUNC) */
  3520. if (sattr->ia_valid & ATTR_OPEN)
  3521. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
  3522. /* Optimization: if the end result is no change, don't RPC */
  3523. if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  3524. return 0;
  3525. /* Search for an existing open(O_WRITE) file */
  3526. if (sattr->ia_valid & ATTR_FILE) {
  3527. ctx = nfs_file_open_context(sattr->ia_file);
  3528. if (ctx)
  3529. cred = ctx->cred;
  3530. }
  3531. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  3532. if (IS_ERR(label))
  3533. return PTR_ERR(label);
  3534. /* Return any delegations if we're going to change ACLs */
  3535. if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  3536. nfs4_inode_make_writeable(inode);
  3537. status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
  3538. if (status == 0) {
  3539. nfs_setattr_update_inode(inode, sattr, fattr);
  3540. nfs_setsecurity(inode, fattr, label);
  3541. }
  3542. nfs4_label_free(label);
  3543. return status;
  3544. }
  3545. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  3546. const struct qstr *name, struct nfs_fh *fhandle,
  3547. struct nfs_fattr *fattr, struct nfs4_label *label)
  3548. {
  3549. struct nfs_server *server = NFS_SERVER(dir);
  3550. int status;
  3551. struct nfs4_lookup_arg args = {
  3552. .bitmask = server->attr_bitmask,
  3553. .dir_fh = NFS_FH(dir),
  3554. .name = name,
  3555. };
  3556. struct nfs4_lookup_res res = {
  3557. .server = server,
  3558. .fattr = fattr,
  3559. .label = label,
  3560. .fh = fhandle,
  3561. };
  3562. struct rpc_message msg = {
  3563. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  3564. .rpc_argp = &args,
  3565. .rpc_resp = &res,
  3566. };
  3567. args.bitmask = nfs4_bitmask(server, label);
  3568. nfs_fattr_init(fattr);
  3569. dprintk("NFS call lookup %s\n", name->name);
  3570. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  3571. dprintk("NFS reply lookup: %d\n", status);
  3572. return status;
  3573. }
  3574. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  3575. {
  3576. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3577. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  3578. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  3579. fattr->nlink = 2;
  3580. }
  3581. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  3582. const struct qstr *name, struct nfs_fh *fhandle,
  3583. struct nfs_fattr *fattr, struct nfs4_label *label)
  3584. {
  3585. struct nfs4_exception exception = { };
  3586. struct rpc_clnt *client = *clnt;
  3587. int err;
  3588. do {
  3589. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
  3590. trace_nfs4_lookup(dir, name, err);
  3591. switch (err) {
  3592. case -NFS4ERR_BADNAME:
  3593. err = -ENOENT;
  3594. goto out;
  3595. case -NFS4ERR_MOVED:
  3596. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  3597. if (err == -NFS4ERR_MOVED)
  3598. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  3599. goto out;
  3600. case -NFS4ERR_WRONGSEC:
  3601. err = -EPERM;
  3602. if (client != *clnt)
  3603. goto out;
  3604. client = nfs4_negotiate_security(client, dir, name);
  3605. if (IS_ERR(client))
  3606. return PTR_ERR(client);
  3607. exception.retry = 1;
  3608. break;
  3609. default:
  3610. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  3611. }
  3612. } while (exception.retry);
  3613. out:
  3614. if (err == 0)
  3615. *clnt = client;
  3616. else if (client != *clnt)
  3617. rpc_shutdown_client(client);
  3618. return err;
  3619. }
  3620. static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  3621. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  3622. struct nfs4_label *label)
  3623. {
  3624. int status;
  3625. struct rpc_clnt *client = NFS_CLIENT(dir);
  3626. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
  3627. if (client != NFS_CLIENT(dir)) {
  3628. rpc_shutdown_client(client);
  3629. nfs_fixup_secinfo_attributes(fattr);
  3630. }
  3631. return status;
  3632. }
  3633. struct rpc_clnt *
  3634. nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
  3635. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  3636. {
  3637. struct rpc_clnt *client = NFS_CLIENT(dir);
  3638. int status;
  3639. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
  3640. if (status < 0)
  3641. return ERR_PTR(status);
  3642. return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
  3643. }
  3644. static int _nfs4_proc_lookupp(struct inode *inode,
  3645. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  3646. struct nfs4_label *label)
  3647. {
  3648. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  3649. struct nfs_server *server = NFS_SERVER(inode);
  3650. int status;
  3651. struct nfs4_lookupp_arg args = {
  3652. .bitmask = server->attr_bitmask,
  3653. .fh = NFS_FH(inode),
  3654. };
  3655. struct nfs4_lookupp_res res = {
  3656. .server = server,
  3657. .fattr = fattr,
  3658. .label = label,
  3659. .fh = fhandle,
  3660. };
  3661. struct rpc_message msg = {
  3662. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
  3663. .rpc_argp = &args,
  3664. .rpc_resp = &res,
  3665. };
  3666. args.bitmask = nfs4_bitmask(server, label);
  3667. nfs_fattr_init(fattr);
  3668. dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
  3669. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
  3670. &res.seq_res, 0);
  3671. dprintk("NFS reply lookupp: %d\n", status);
  3672. return status;
  3673. }
  3674. static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
  3675. struct nfs_fattr *fattr, struct nfs4_label *label)
  3676. {
  3677. struct nfs4_exception exception = { };
  3678. int err;
  3679. do {
  3680. err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
  3681. trace_nfs4_lookupp(inode, err);
  3682. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3683. &exception);
  3684. } while (exception.retry);
  3685. return err;
  3686. }
  3687. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  3688. {
  3689. struct nfs_server *server = NFS_SERVER(inode);
  3690. struct nfs4_accessargs args = {
  3691. .fh = NFS_FH(inode),
  3692. .access = entry->mask,
  3693. };
  3694. struct nfs4_accessres res = {
  3695. .server = server,
  3696. };
  3697. struct rpc_message msg = {
  3698. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  3699. .rpc_argp = &args,
  3700. .rpc_resp = &res,
  3701. .rpc_cred = entry->cred,
  3702. };
  3703. int status = 0;
  3704. if (!nfs_have_delegated_attributes(inode)) {
  3705. res.fattr = nfs_alloc_fattr();
  3706. if (res.fattr == NULL)
  3707. return -ENOMEM;
  3708. args.bitmask = server->cache_consistency_bitmask;
  3709. }
  3710. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3711. if (!status) {
  3712. nfs_access_set_mask(entry, res.access);
  3713. if (res.fattr)
  3714. nfs_refresh_inode(inode, res.fattr);
  3715. }
  3716. nfs_free_fattr(res.fattr);
  3717. return status;
  3718. }
  3719. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  3720. {
  3721. struct nfs4_exception exception = { };
  3722. int err;
  3723. do {
  3724. err = _nfs4_proc_access(inode, entry);
  3725. trace_nfs4_access(inode, err);
  3726. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3727. &exception);
  3728. } while (exception.retry);
  3729. return err;
  3730. }
  3731. /*
  3732. * TODO: For the time being, we don't try to get any attributes
  3733. * along with any of the zero-copy operations READ, READDIR,
  3734. * READLINK, WRITE.
  3735. *
  3736. * In the case of the first three, we want to put the GETATTR
  3737. * after the read-type operation -- this is because it is hard
  3738. * to predict the length of a GETATTR response in v4, and thus
  3739. * align the READ data correctly. This means that the GETATTR
  3740. * may end up partially falling into the page cache, and we should
  3741. * shift it into the 'tail' of the xdr_buf before processing.
  3742. * To do this efficiently, we need to know the total length
  3743. * of data received, which doesn't seem to be available outside
  3744. * of the RPC layer.
  3745. *
  3746. * In the case of WRITE, we also want to put the GETATTR after
  3747. * the operation -- in this case because we want to make sure
  3748. * we get the post-operation mtime and size.
  3749. *
  3750. * Both of these changes to the XDR layer would in fact be quite
  3751. * minor, but I decided to leave them for a subsequent patch.
  3752. */
  3753. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  3754. unsigned int pgbase, unsigned int pglen)
  3755. {
  3756. struct nfs4_readlink args = {
  3757. .fh = NFS_FH(inode),
  3758. .pgbase = pgbase,
  3759. .pglen = pglen,
  3760. .pages = &page,
  3761. };
  3762. struct nfs4_readlink_res res;
  3763. struct rpc_message msg = {
  3764. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  3765. .rpc_argp = &args,
  3766. .rpc_resp = &res,
  3767. };
  3768. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  3769. }
  3770. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  3771. unsigned int pgbase, unsigned int pglen)
  3772. {
  3773. struct nfs4_exception exception = { };
  3774. int err;
  3775. do {
  3776. err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
  3777. trace_nfs4_readlink(inode, err);
  3778. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3779. &exception);
  3780. } while (exception.retry);
  3781. return err;
  3782. }
  3783. /*
  3784. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  3785. */
  3786. static int
  3787. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  3788. int flags)
  3789. {
  3790. struct nfs_server *server = NFS_SERVER(dir);
  3791. struct nfs4_label l, *ilabel = NULL;
  3792. struct nfs_open_context *ctx;
  3793. struct nfs4_state *state;
  3794. int status = 0;
  3795. ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
  3796. if (IS_ERR(ctx))
  3797. return PTR_ERR(ctx);
  3798. ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
  3799. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  3800. sattr->ia_mode &= ~current_umask();
  3801. state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
  3802. if (IS_ERR(state)) {
  3803. status = PTR_ERR(state);
  3804. goto out;
  3805. }
  3806. out:
  3807. nfs4_label_release_security(ilabel);
  3808. put_nfs_open_context(ctx);
  3809. return status;
  3810. }
  3811. static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
  3812. {
  3813. struct nfs_server *server = NFS_SERVER(dir);
  3814. struct nfs_removeargs args = {
  3815. .fh = NFS_FH(dir),
  3816. .name = *name,
  3817. };
  3818. struct nfs_removeres res = {
  3819. .server = server,
  3820. };
  3821. struct rpc_message msg = {
  3822. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  3823. .rpc_argp = &args,
  3824. .rpc_resp = &res,
  3825. };
  3826. unsigned long timestamp = jiffies;
  3827. int status;
  3828. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  3829. if (status == 0)
  3830. update_changeattr(dir, &res.cinfo, timestamp);
  3831. return status;
  3832. }
  3833. static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
  3834. {
  3835. struct nfs4_exception exception = { };
  3836. struct inode *inode = d_inode(dentry);
  3837. int err;
  3838. if (inode) {
  3839. if (inode->i_nlink == 1)
  3840. nfs4_inode_return_delegation(inode);
  3841. else
  3842. nfs4_inode_make_writeable(inode);
  3843. }
  3844. do {
  3845. err = _nfs4_proc_remove(dir, &dentry->d_name);
  3846. trace_nfs4_remove(dir, &dentry->d_name, err);
  3847. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3848. &exception);
  3849. } while (exception.retry);
  3850. return err;
  3851. }
  3852. static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
  3853. {
  3854. struct nfs4_exception exception = { };
  3855. int err;
  3856. do {
  3857. err = _nfs4_proc_remove(dir, name);
  3858. trace_nfs4_remove(dir, name, err);
  3859. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3860. &exception);
  3861. } while (exception.retry);
  3862. return err;
  3863. }
  3864. static void nfs4_proc_unlink_setup(struct rpc_message *msg,
  3865. struct dentry *dentry,
  3866. struct inode *inode)
  3867. {
  3868. struct nfs_removeargs *args = msg->rpc_argp;
  3869. struct nfs_removeres *res = msg->rpc_resp;
  3870. res->server = NFS_SB(dentry->d_sb);
  3871. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  3872. nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
  3873. nfs_fattr_init(res->dir_attr);
  3874. if (inode)
  3875. nfs4_inode_return_delegation(inode);
  3876. }
  3877. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  3878. {
  3879. nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
  3880. &data->args.seq_args,
  3881. &data->res.seq_res,
  3882. task);
  3883. }
  3884. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  3885. {
  3886. struct nfs_unlinkdata *data = task->tk_calldata;
  3887. struct nfs_removeres *res = &data->res;
  3888. if (!nfs4_sequence_done(task, &res->seq_res))
  3889. return 0;
  3890. if (nfs4_async_handle_error(task, res->server, NULL,
  3891. &data->timeout) == -EAGAIN)
  3892. return 0;
  3893. if (task->tk_status == 0)
  3894. update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
  3895. return 1;
  3896. }
  3897. static void nfs4_proc_rename_setup(struct rpc_message *msg,
  3898. struct dentry *old_dentry,
  3899. struct dentry *new_dentry)
  3900. {
  3901. struct nfs_renameargs *arg = msg->rpc_argp;
  3902. struct nfs_renameres *res = msg->rpc_resp;
  3903. struct inode *old_inode = d_inode(old_dentry);
  3904. struct inode *new_inode = d_inode(new_dentry);
  3905. if (old_inode)
  3906. nfs4_inode_make_writeable(old_inode);
  3907. if (new_inode)
  3908. nfs4_inode_return_delegation(new_inode);
  3909. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  3910. res->server = NFS_SB(old_dentry->d_sb);
  3911. nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
  3912. }
  3913. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  3914. {
  3915. nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
  3916. &data->args.seq_args,
  3917. &data->res.seq_res,
  3918. task);
  3919. }
  3920. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  3921. struct inode *new_dir)
  3922. {
  3923. struct nfs_renamedata *data = task->tk_calldata;
  3924. struct nfs_renameres *res = &data->res;
  3925. if (!nfs4_sequence_done(task, &res->seq_res))
  3926. return 0;
  3927. if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
  3928. return 0;
  3929. if (task->tk_status == 0) {
  3930. update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
  3931. if (new_dir != old_dir)
  3932. update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
  3933. }
  3934. return 1;
  3935. }
  3936. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  3937. {
  3938. struct nfs_server *server = NFS_SERVER(inode);
  3939. __u32 bitmask[NFS4_BITMASK_SZ];
  3940. struct nfs4_link_arg arg = {
  3941. .fh = NFS_FH(inode),
  3942. .dir_fh = NFS_FH(dir),
  3943. .name = name,
  3944. .bitmask = bitmask,
  3945. };
  3946. struct nfs4_link_res res = {
  3947. .server = server,
  3948. .label = NULL,
  3949. };
  3950. struct rpc_message msg = {
  3951. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  3952. .rpc_argp = &arg,
  3953. .rpc_resp = &res,
  3954. };
  3955. int status = -ENOMEM;
  3956. res.fattr = nfs_alloc_fattr();
  3957. if (res.fattr == NULL)
  3958. goto out;
  3959. res.label = nfs4_label_alloc(server, GFP_KERNEL);
  3960. if (IS_ERR(res.label)) {
  3961. status = PTR_ERR(res.label);
  3962. goto out;
  3963. }
  3964. nfs4_inode_make_writeable(inode);
  3965. nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
  3966. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3967. if (!status) {
  3968. update_changeattr(dir, &res.cinfo, res.fattr->time_start);
  3969. status = nfs_post_op_update_inode(inode, res.fattr);
  3970. if (!status)
  3971. nfs_setsecurity(inode, res.fattr, res.label);
  3972. }
  3973. nfs4_label_free(res.label);
  3974. out:
  3975. nfs_free_fattr(res.fattr);
  3976. return status;
  3977. }
  3978. static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  3979. {
  3980. struct nfs4_exception exception = { };
  3981. int err;
  3982. do {
  3983. err = nfs4_handle_exception(NFS_SERVER(inode),
  3984. _nfs4_proc_link(inode, dir, name),
  3985. &exception);
  3986. } while (exception.retry);
  3987. return err;
  3988. }
  3989. struct nfs4_createdata {
  3990. struct rpc_message msg;
  3991. struct nfs4_create_arg arg;
  3992. struct nfs4_create_res res;
  3993. struct nfs_fh fh;
  3994. struct nfs_fattr fattr;
  3995. struct nfs4_label *label;
  3996. };
  3997. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  3998. const struct qstr *name, struct iattr *sattr, u32 ftype)
  3999. {
  4000. struct nfs4_createdata *data;
  4001. data = kzalloc(sizeof(*data), GFP_KERNEL);
  4002. if (data != NULL) {
  4003. struct nfs_server *server = NFS_SERVER(dir);
  4004. data->label = nfs4_label_alloc(server, GFP_KERNEL);
  4005. if (IS_ERR(data->label))
  4006. goto out_free;
  4007. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  4008. data->msg.rpc_argp = &data->arg;
  4009. data->msg.rpc_resp = &data->res;
  4010. data->arg.dir_fh = NFS_FH(dir);
  4011. data->arg.server = server;
  4012. data->arg.name = name;
  4013. data->arg.attrs = sattr;
  4014. data->arg.ftype = ftype;
  4015. data->arg.bitmask = nfs4_bitmask(server, data->label);
  4016. data->arg.umask = current_umask();
  4017. data->res.server = server;
  4018. data->res.fh = &data->fh;
  4019. data->res.fattr = &data->fattr;
  4020. data->res.label = data->label;
  4021. nfs_fattr_init(data->res.fattr);
  4022. }
  4023. return data;
  4024. out_free:
  4025. kfree(data);
  4026. return NULL;
  4027. }
  4028. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  4029. {
  4030. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  4031. &data->arg.seq_args, &data->res.seq_res, 1);
  4032. if (status == 0) {
  4033. update_changeattr(dir, &data->res.dir_cinfo,
  4034. data->res.fattr->time_start);
  4035. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
  4036. }
  4037. return status;
  4038. }
  4039. static void nfs4_free_createdata(struct nfs4_createdata *data)
  4040. {
  4041. nfs4_label_free(data->label);
  4042. kfree(data);
  4043. }
  4044. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  4045. struct page *page, unsigned int len, struct iattr *sattr,
  4046. struct nfs4_label *label)
  4047. {
  4048. struct nfs4_createdata *data;
  4049. int status = -ENAMETOOLONG;
  4050. if (len > NFS4_MAXPATHLEN)
  4051. goto out;
  4052. status = -ENOMEM;
  4053. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  4054. if (data == NULL)
  4055. goto out;
  4056. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  4057. data->arg.u.symlink.pages = &page;
  4058. data->arg.u.symlink.len = len;
  4059. data->arg.label = label;
  4060. status = nfs4_do_create(dir, dentry, data);
  4061. nfs4_free_createdata(data);
  4062. out:
  4063. return status;
  4064. }
  4065. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  4066. struct page *page, unsigned int len, struct iattr *sattr)
  4067. {
  4068. struct nfs4_exception exception = { };
  4069. struct nfs4_label l, *label = NULL;
  4070. int err;
  4071. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  4072. do {
  4073. err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
  4074. trace_nfs4_symlink(dir, &dentry->d_name, err);
  4075. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4076. &exception);
  4077. } while (exception.retry);
  4078. nfs4_label_release_security(label);
  4079. return err;
  4080. }
  4081. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  4082. struct iattr *sattr, struct nfs4_label *label)
  4083. {
  4084. struct nfs4_createdata *data;
  4085. int status = -ENOMEM;
  4086. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  4087. if (data == NULL)
  4088. goto out;
  4089. data->arg.label = label;
  4090. status = nfs4_do_create(dir, dentry, data);
  4091. nfs4_free_createdata(data);
  4092. out:
  4093. return status;
  4094. }
  4095. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  4096. struct iattr *sattr)
  4097. {
  4098. struct nfs_server *server = NFS_SERVER(dir);
  4099. struct nfs4_exception exception = { };
  4100. struct nfs4_label l, *label = NULL;
  4101. int err;
  4102. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  4103. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  4104. sattr->ia_mode &= ~current_umask();
  4105. do {
  4106. err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
  4107. trace_nfs4_mkdir(dir, &dentry->d_name, err);
  4108. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4109. &exception);
  4110. } while (exception.retry);
  4111. nfs4_label_release_security(label);
  4112. return err;
  4113. }
  4114. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  4115. u64 cookie, struct page **pages, unsigned int count, bool plus)
  4116. {
  4117. struct inode *dir = d_inode(dentry);
  4118. struct nfs4_readdir_arg args = {
  4119. .fh = NFS_FH(dir),
  4120. .pages = pages,
  4121. .pgbase = 0,
  4122. .count = count,
  4123. .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
  4124. .plus = plus,
  4125. };
  4126. struct nfs4_readdir_res res;
  4127. struct rpc_message msg = {
  4128. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  4129. .rpc_argp = &args,
  4130. .rpc_resp = &res,
  4131. .rpc_cred = cred,
  4132. };
  4133. int status;
  4134. dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
  4135. dentry,
  4136. (unsigned long long)cookie);
  4137. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  4138. res.pgbase = args.pgbase;
  4139. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  4140. if (status >= 0) {
  4141. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  4142. status += args.pgbase;
  4143. }
  4144. nfs_invalidate_atime(dir);
  4145. dprintk("%s: returns %d\n", __func__, status);
  4146. return status;
  4147. }
  4148. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  4149. u64 cookie, struct page **pages, unsigned int count, bool plus)
  4150. {
  4151. struct nfs4_exception exception = { };
  4152. int err;
  4153. do {
  4154. err = _nfs4_proc_readdir(dentry, cred, cookie,
  4155. pages, count, plus);
  4156. trace_nfs4_readdir(d_inode(dentry), err);
  4157. err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
  4158. &exception);
  4159. } while (exception.retry);
  4160. return err;
  4161. }
  4162. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  4163. struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
  4164. {
  4165. struct nfs4_createdata *data;
  4166. int mode = sattr->ia_mode;
  4167. int status = -ENOMEM;
  4168. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  4169. if (data == NULL)
  4170. goto out;
  4171. if (S_ISFIFO(mode))
  4172. data->arg.ftype = NF4FIFO;
  4173. else if (S_ISBLK(mode)) {
  4174. data->arg.ftype = NF4BLK;
  4175. data->arg.u.device.specdata1 = MAJOR(rdev);
  4176. data->arg.u.device.specdata2 = MINOR(rdev);
  4177. }
  4178. else if (S_ISCHR(mode)) {
  4179. data->arg.ftype = NF4CHR;
  4180. data->arg.u.device.specdata1 = MAJOR(rdev);
  4181. data->arg.u.device.specdata2 = MINOR(rdev);
  4182. } else if (!S_ISSOCK(mode)) {
  4183. status = -EINVAL;
  4184. goto out_free;
  4185. }
  4186. data->arg.label = label;
  4187. status = nfs4_do_create(dir, dentry, data);
  4188. out_free:
  4189. nfs4_free_createdata(data);
  4190. out:
  4191. return status;
  4192. }
  4193. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  4194. struct iattr *sattr, dev_t rdev)
  4195. {
  4196. struct nfs_server *server = NFS_SERVER(dir);
  4197. struct nfs4_exception exception = { };
  4198. struct nfs4_label l, *label = NULL;
  4199. int err;
  4200. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  4201. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  4202. sattr->ia_mode &= ~current_umask();
  4203. do {
  4204. err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
  4205. trace_nfs4_mknod(dir, &dentry->d_name, err);
  4206. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4207. &exception);
  4208. } while (exception.retry);
  4209. nfs4_label_release_security(label);
  4210. return err;
  4211. }
  4212. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  4213. struct nfs_fsstat *fsstat)
  4214. {
  4215. struct nfs4_statfs_arg args = {
  4216. .fh = fhandle,
  4217. .bitmask = server->attr_bitmask,
  4218. };
  4219. struct nfs4_statfs_res res = {
  4220. .fsstat = fsstat,
  4221. };
  4222. struct rpc_message msg = {
  4223. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  4224. .rpc_argp = &args,
  4225. .rpc_resp = &res,
  4226. };
  4227. nfs_fattr_init(fsstat->fattr);
  4228. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4229. }
  4230. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  4231. {
  4232. struct nfs4_exception exception = { };
  4233. int err;
  4234. do {
  4235. err = nfs4_handle_exception(server,
  4236. _nfs4_proc_statfs(server, fhandle, fsstat),
  4237. &exception);
  4238. } while (exception.retry);
  4239. return err;
  4240. }
  4241. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  4242. struct nfs_fsinfo *fsinfo)
  4243. {
  4244. struct nfs4_fsinfo_arg args = {
  4245. .fh = fhandle,
  4246. .bitmask = server->attr_bitmask,
  4247. };
  4248. struct nfs4_fsinfo_res res = {
  4249. .fsinfo = fsinfo,
  4250. };
  4251. struct rpc_message msg = {
  4252. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  4253. .rpc_argp = &args,
  4254. .rpc_resp = &res,
  4255. };
  4256. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4257. }
  4258. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4259. {
  4260. struct nfs4_exception exception = { };
  4261. unsigned long now = jiffies;
  4262. int err;
  4263. do {
  4264. err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
  4265. trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
  4266. if (err == 0) {
  4267. nfs4_set_lease_period(server->nfs_client,
  4268. fsinfo->lease_time * HZ,
  4269. now);
  4270. break;
  4271. }
  4272. err = nfs4_handle_exception(server, err, &exception);
  4273. } while (exception.retry);
  4274. return err;
  4275. }
  4276. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4277. {
  4278. int error;
  4279. nfs_fattr_init(fsinfo->fattr);
  4280. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  4281. if (error == 0) {
  4282. /* block layout checks this! */
  4283. server->pnfs_blksize = fsinfo->blksize;
  4284. set_pnfs_layoutdriver(server, fhandle, fsinfo);
  4285. }
  4286. return error;
  4287. }
  4288. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4289. struct nfs_pathconf *pathconf)
  4290. {
  4291. struct nfs4_pathconf_arg args = {
  4292. .fh = fhandle,
  4293. .bitmask = server->attr_bitmask,
  4294. };
  4295. struct nfs4_pathconf_res res = {
  4296. .pathconf = pathconf,
  4297. };
  4298. struct rpc_message msg = {
  4299. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  4300. .rpc_argp = &args,
  4301. .rpc_resp = &res,
  4302. };
  4303. /* None of the pathconf attributes are mandatory to implement */
  4304. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  4305. memset(pathconf, 0, sizeof(*pathconf));
  4306. return 0;
  4307. }
  4308. nfs_fattr_init(pathconf->fattr);
  4309. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4310. }
  4311. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4312. struct nfs_pathconf *pathconf)
  4313. {
  4314. struct nfs4_exception exception = { };
  4315. int err;
  4316. do {
  4317. err = nfs4_handle_exception(server,
  4318. _nfs4_proc_pathconf(server, fhandle, pathconf),
  4319. &exception);
  4320. } while (exception.retry);
  4321. return err;
  4322. }
  4323. int nfs4_set_rw_stateid(nfs4_stateid *stateid,
  4324. const struct nfs_open_context *ctx,
  4325. const struct nfs_lock_context *l_ctx,
  4326. fmode_t fmode)
  4327. {
  4328. return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
  4329. }
  4330. EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
  4331. static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
  4332. const struct nfs_open_context *ctx,
  4333. const struct nfs_lock_context *l_ctx,
  4334. fmode_t fmode)
  4335. {
  4336. nfs4_stateid current_stateid;
  4337. /* If the current stateid represents a lost lock, then exit */
  4338. if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
  4339. return true;
  4340. return nfs4_stateid_match(stateid, &current_stateid);
  4341. }
  4342. static bool nfs4_error_stateid_expired(int err)
  4343. {
  4344. switch (err) {
  4345. case -NFS4ERR_DELEG_REVOKED:
  4346. case -NFS4ERR_ADMIN_REVOKED:
  4347. case -NFS4ERR_BAD_STATEID:
  4348. case -NFS4ERR_STALE_STATEID:
  4349. case -NFS4ERR_OLD_STATEID:
  4350. case -NFS4ERR_OPENMODE:
  4351. case -NFS4ERR_EXPIRED:
  4352. return true;
  4353. }
  4354. return false;
  4355. }
  4356. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4357. {
  4358. struct nfs_server *server = NFS_SERVER(hdr->inode);
  4359. trace_nfs4_read(hdr, task->tk_status);
  4360. if (task->tk_status < 0) {
  4361. struct nfs4_exception exception = {
  4362. .inode = hdr->inode,
  4363. .state = hdr->args.context->state,
  4364. .stateid = &hdr->args.stateid,
  4365. };
  4366. task->tk_status = nfs4_async_handle_exception(task,
  4367. server, task->tk_status, &exception);
  4368. if (exception.retry) {
  4369. rpc_restart_call_prepare(task);
  4370. return -EAGAIN;
  4371. }
  4372. }
  4373. if (task->tk_status > 0)
  4374. renew_lease(server, hdr->timestamp);
  4375. return 0;
  4376. }
  4377. static bool nfs4_read_stateid_changed(struct rpc_task *task,
  4378. struct nfs_pgio_args *args)
  4379. {
  4380. if (!nfs4_error_stateid_expired(task->tk_status) ||
  4381. nfs4_stateid_is_current(&args->stateid,
  4382. args->context,
  4383. args->lock_context,
  4384. FMODE_READ))
  4385. return false;
  4386. rpc_restart_call_prepare(task);
  4387. return true;
  4388. }
  4389. static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4390. {
  4391. dprintk("--> %s\n", __func__);
  4392. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  4393. return -EAGAIN;
  4394. if (nfs4_read_stateid_changed(task, &hdr->args))
  4395. return -EAGAIN;
  4396. if (task->tk_status > 0)
  4397. nfs_invalidate_atime(hdr->inode);
  4398. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  4399. nfs4_read_done_cb(task, hdr);
  4400. }
  4401. static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
  4402. struct rpc_message *msg)
  4403. {
  4404. hdr->timestamp = jiffies;
  4405. if (!hdr->pgio_done_cb)
  4406. hdr->pgio_done_cb = nfs4_read_done_cb;
  4407. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  4408. nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
  4409. }
  4410. static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
  4411. struct nfs_pgio_header *hdr)
  4412. {
  4413. if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
  4414. &hdr->args.seq_args,
  4415. &hdr->res.seq_res,
  4416. task))
  4417. return 0;
  4418. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  4419. hdr->args.lock_context,
  4420. hdr->rw_mode) == -EIO)
  4421. return -EIO;
  4422. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
  4423. return -EIO;
  4424. return 0;
  4425. }
  4426. static int nfs4_write_done_cb(struct rpc_task *task,
  4427. struct nfs_pgio_header *hdr)
  4428. {
  4429. struct inode *inode = hdr->inode;
  4430. trace_nfs4_write(hdr, task->tk_status);
  4431. if (task->tk_status < 0) {
  4432. struct nfs4_exception exception = {
  4433. .inode = hdr->inode,
  4434. .state = hdr->args.context->state,
  4435. .stateid = &hdr->args.stateid,
  4436. };
  4437. task->tk_status = nfs4_async_handle_exception(task,
  4438. NFS_SERVER(inode), task->tk_status,
  4439. &exception);
  4440. if (exception.retry) {
  4441. rpc_restart_call_prepare(task);
  4442. return -EAGAIN;
  4443. }
  4444. }
  4445. if (task->tk_status >= 0) {
  4446. renew_lease(NFS_SERVER(inode), hdr->timestamp);
  4447. nfs_writeback_update_inode(hdr);
  4448. }
  4449. return 0;
  4450. }
  4451. static bool nfs4_write_stateid_changed(struct rpc_task *task,
  4452. struct nfs_pgio_args *args)
  4453. {
  4454. if (!nfs4_error_stateid_expired(task->tk_status) ||
  4455. nfs4_stateid_is_current(&args->stateid,
  4456. args->context,
  4457. args->lock_context,
  4458. FMODE_WRITE))
  4459. return false;
  4460. rpc_restart_call_prepare(task);
  4461. return true;
  4462. }
  4463. static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4464. {
  4465. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  4466. return -EAGAIN;
  4467. if (nfs4_write_stateid_changed(task, &hdr->args))
  4468. return -EAGAIN;
  4469. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  4470. nfs4_write_done_cb(task, hdr);
  4471. }
  4472. static
  4473. bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
  4474. {
  4475. /* Don't request attributes for pNFS or O_DIRECT writes */
  4476. if (hdr->ds_clp != NULL || hdr->dreq != NULL)
  4477. return false;
  4478. /* Otherwise, request attributes if and only if we don't hold
  4479. * a delegation
  4480. */
  4481. return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
  4482. }
  4483. static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
  4484. struct rpc_message *msg,
  4485. struct rpc_clnt **clnt)
  4486. {
  4487. struct nfs_server *server = NFS_SERVER(hdr->inode);
  4488. if (!nfs4_write_need_cache_consistency_data(hdr)) {
  4489. hdr->args.bitmask = NULL;
  4490. hdr->res.fattr = NULL;
  4491. } else
  4492. hdr->args.bitmask = server->cache_consistency_bitmask;
  4493. if (!hdr->pgio_done_cb)
  4494. hdr->pgio_done_cb = nfs4_write_done_cb;
  4495. hdr->res.server = server;
  4496. hdr->timestamp = jiffies;
  4497. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  4498. nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
  4499. nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
  4500. }
  4501. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  4502. {
  4503. nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
  4504. &data->args.seq_args,
  4505. &data->res.seq_res,
  4506. task);
  4507. }
  4508. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  4509. {
  4510. struct inode *inode = data->inode;
  4511. trace_nfs4_commit(data, task->tk_status);
  4512. if (nfs4_async_handle_error(task, NFS_SERVER(inode),
  4513. NULL, NULL) == -EAGAIN) {
  4514. rpc_restart_call_prepare(task);
  4515. return -EAGAIN;
  4516. }
  4517. return 0;
  4518. }
  4519. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  4520. {
  4521. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4522. return -EAGAIN;
  4523. return data->commit_done_cb(task, data);
  4524. }
  4525. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
  4526. struct rpc_clnt **clnt)
  4527. {
  4528. struct nfs_server *server = NFS_SERVER(data->inode);
  4529. if (data->commit_done_cb == NULL)
  4530. data->commit_done_cb = nfs4_commit_done_cb;
  4531. data->res.server = server;
  4532. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  4533. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
  4534. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
  4535. }
  4536. struct nfs4_renewdata {
  4537. struct nfs_client *client;
  4538. unsigned long timestamp;
  4539. };
  4540. /*
  4541. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  4542. * standalone procedure for queueing an asynchronous RENEW.
  4543. */
  4544. static void nfs4_renew_release(void *calldata)
  4545. {
  4546. struct nfs4_renewdata *data = calldata;
  4547. struct nfs_client *clp = data->client;
  4548. if (refcount_read(&clp->cl_count) > 1)
  4549. nfs4_schedule_state_renewal(clp);
  4550. nfs_put_client(clp);
  4551. kfree(data);
  4552. }
  4553. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  4554. {
  4555. struct nfs4_renewdata *data = calldata;
  4556. struct nfs_client *clp = data->client;
  4557. unsigned long timestamp = data->timestamp;
  4558. trace_nfs4_renew_async(clp, task->tk_status);
  4559. switch (task->tk_status) {
  4560. case 0:
  4561. break;
  4562. case -NFS4ERR_LEASE_MOVED:
  4563. nfs4_schedule_lease_moved_recovery(clp);
  4564. break;
  4565. default:
  4566. /* Unless we're shutting down, schedule state recovery! */
  4567. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  4568. return;
  4569. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  4570. nfs4_schedule_lease_recovery(clp);
  4571. return;
  4572. }
  4573. nfs4_schedule_path_down_recovery(clp);
  4574. }
  4575. do_renew_lease(clp, timestamp);
  4576. }
  4577. static const struct rpc_call_ops nfs4_renew_ops = {
  4578. .rpc_call_done = nfs4_renew_done,
  4579. .rpc_release = nfs4_renew_release,
  4580. };
  4581. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  4582. {
  4583. struct rpc_message msg = {
  4584. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  4585. .rpc_argp = clp,
  4586. .rpc_cred = cred,
  4587. };
  4588. struct nfs4_renewdata *data;
  4589. if (renew_flags == 0)
  4590. return 0;
  4591. if (!refcount_inc_not_zero(&clp->cl_count))
  4592. return -EIO;
  4593. data = kmalloc(sizeof(*data), GFP_NOFS);
  4594. if (data == NULL) {
  4595. nfs_put_client(clp);
  4596. return -ENOMEM;
  4597. }
  4598. data->client = clp;
  4599. data->timestamp = jiffies;
  4600. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
  4601. &nfs4_renew_ops, data);
  4602. }
  4603. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  4604. {
  4605. struct rpc_message msg = {
  4606. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  4607. .rpc_argp = clp,
  4608. .rpc_cred = cred,
  4609. };
  4610. unsigned long now = jiffies;
  4611. int status;
  4612. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4613. if (status < 0)
  4614. return status;
  4615. do_renew_lease(clp, now);
  4616. return 0;
  4617. }
  4618. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  4619. {
  4620. return server->caps & NFS_CAP_ACLS;
  4621. }
  4622. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  4623. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  4624. * the stack.
  4625. */
  4626. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  4627. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  4628. struct page **pages)
  4629. {
  4630. struct page *newpage, **spages;
  4631. int rc = 0;
  4632. size_t len;
  4633. spages = pages;
  4634. do {
  4635. len = min_t(size_t, PAGE_SIZE, buflen);
  4636. newpage = alloc_page(GFP_KERNEL);
  4637. if (newpage == NULL)
  4638. goto unwind;
  4639. memcpy(page_address(newpage), buf, len);
  4640. buf += len;
  4641. buflen -= len;
  4642. *pages++ = newpage;
  4643. rc++;
  4644. } while (buflen != 0);
  4645. return rc;
  4646. unwind:
  4647. for(; rc > 0; rc--)
  4648. __free_page(spages[rc-1]);
  4649. return -ENOMEM;
  4650. }
  4651. struct nfs4_cached_acl {
  4652. int cached;
  4653. size_t len;
  4654. char data[0];
  4655. };
  4656. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  4657. {
  4658. struct nfs_inode *nfsi = NFS_I(inode);
  4659. spin_lock(&inode->i_lock);
  4660. kfree(nfsi->nfs4_acl);
  4661. nfsi->nfs4_acl = acl;
  4662. spin_unlock(&inode->i_lock);
  4663. }
  4664. static void nfs4_zap_acl_attr(struct inode *inode)
  4665. {
  4666. nfs4_set_cached_acl(inode, NULL);
  4667. }
  4668. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  4669. {
  4670. struct nfs_inode *nfsi = NFS_I(inode);
  4671. struct nfs4_cached_acl *acl;
  4672. int ret = -ENOENT;
  4673. spin_lock(&inode->i_lock);
  4674. acl = nfsi->nfs4_acl;
  4675. if (acl == NULL)
  4676. goto out;
  4677. if (buf == NULL) /* user is just asking for length */
  4678. goto out_len;
  4679. if (acl->cached == 0)
  4680. goto out;
  4681. ret = -ERANGE; /* see getxattr(2) man page */
  4682. if (acl->len > buflen)
  4683. goto out;
  4684. memcpy(buf, acl->data, acl->len);
  4685. out_len:
  4686. ret = acl->len;
  4687. out:
  4688. spin_unlock(&inode->i_lock);
  4689. return ret;
  4690. }
  4691. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  4692. {
  4693. struct nfs4_cached_acl *acl;
  4694. size_t buflen = sizeof(*acl) + acl_len;
  4695. if (buflen <= PAGE_SIZE) {
  4696. acl = kmalloc(buflen, GFP_KERNEL);
  4697. if (acl == NULL)
  4698. goto out;
  4699. acl->cached = 1;
  4700. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  4701. } else {
  4702. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  4703. if (acl == NULL)
  4704. goto out;
  4705. acl->cached = 0;
  4706. }
  4707. acl->len = acl_len;
  4708. out:
  4709. nfs4_set_cached_acl(inode, acl);
  4710. }
  4711. /*
  4712. * The getxattr API returns the required buffer length when called with a
  4713. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  4714. * the required buf. On a NULL buf, we send a page of data to the server
  4715. * guessing that the ACL request can be serviced by a page. If so, we cache
  4716. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  4717. * the cache. If not so, we throw away the page, and cache the required
  4718. * length. The next getxattr call will then produce another round trip to
  4719. * the server, this time with the input buf of the required size.
  4720. */
  4721. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  4722. {
  4723. struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
  4724. struct nfs_getaclargs args = {
  4725. .fh = NFS_FH(inode),
  4726. .acl_pages = pages,
  4727. .acl_len = buflen,
  4728. };
  4729. struct nfs_getaclres res = {
  4730. .acl_len = buflen,
  4731. };
  4732. struct rpc_message msg = {
  4733. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  4734. .rpc_argp = &args,
  4735. .rpc_resp = &res,
  4736. };
  4737. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
  4738. int ret = -ENOMEM, i;
  4739. if (npages > ARRAY_SIZE(pages))
  4740. return -ERANGE;
  4741. for (i = 0; i < npages; i++) {
  4742. pages[i] = alloc_page(GFP_KERNEL);
  4743. if (!pages[i])
  4744. goto out_free;
  4745. }
  4746. /* for decoding across pages */
  4747. res.acl_scratch = alloc_page(GFP_KERNEL);
  4748. if (!res.acl_scratch)
  4749. goto out_free;
  4750. args.acl_len = npages * PAGE_SIZE;
  4751. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  4752. __func__, buf, buflen, npages, args.acl_len);
  4753. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  4754. &msg, &args.seq_args, &res.seq_res, 0);
  4755. if (ret)
  4756. goto out_free;
  4757. /* Handle the case where the passed-in buffer is too short */
  4758. if (res.acl_flags & NFS4_ACL_TRUNC) {
  4759. /* Did the user only issue a request for the acl length? */
  4760. if (buf == NULL)
  4761. goto out_ok;
  4762. ret = -ERANGE;
  4763. goto out_free;
  4764. }
  4765. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
  4766. if (buf) {
  4767. if (res.acl_len > buflen) {
  4768. ret = -ERANGE;
  4769. goto out_free;
  4770. }
  4771. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  4772. }
  4773. out_ok:
  4774. ret = res.acl_len;
  4775. out_free:
  4776. for (i = 0; i < npages; i++)
  4777. if (pages[i])
  4778. __free_page(pages[i]);
  4779. if (res.acl_scratch)
  4780. __free_page(res.acl_scratch);
  4781. return ret;
  4782. }
  4783. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  4784. {
  4785. struct nfs4_exception exception = { };
  4786. ssize_t ret;
  4787. do {
  4788. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  4789. trace_nfs4_get_acl(inode, ret);
  4790. if (ret >= 0)
  4791. break;
  4792. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  4793. } while (exception.retry);
  4794. return ret;
  4795. }
  4796. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  4797. {
  4798. struct nfs_server *server = NFS_SERVER(inode);
  4799. int ret;
  4800. if (!nfs4_server_supports_acls(server))
  4801. return -EOPNOTSUPP;
  4802. ret = nfs_revalidate_inode(server, inode);
  4803. if (ret < 0)
  4804. return ret;
  4805. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  4806. nfs_zap_acl_cache(inode);
  4807. ret = nfs4_read_cached_acl(inode, buf, buflen);
  4808. if (ret != -ENOENT)
  4809. /* -ENOENT is returned if there is no ACL or if there is an ACL
  4810. * but no cached acl data, just the acl length */
  4811. return ret;
  4812. return nfs4_get_acl_uncached(inode, buf, buflen);
  4813. }
  4814. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  4815. {
  4816. struct nfs_server *server = NFS_SERVER(inode);
  4817. struct page *pages[NFS4ACL_MAXPAGES];
  4818. struct nfs_setaclargs arg = {
  4819. .fh = NFS_FH(inode),
  4820. .acl_pages = pages,
  4821. .acl_len = buflen,
  4822. };
  4823. struct nfs_setaclres res;
  4824. struct rpc_message msg = {
  4825. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  4826. .rpc_argp = &arg,
  4827. .rpc_resp = &res,
  4828. };
  4829. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  4830. int ret, i;
  4831. if (!nfs4_server_supports_acls(server))
  4832. return -EOPNOTSUPP;
  4833. if (npages > ARRAY_SIZE(pages))
  4834. return -ERANGE;
  4835. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
  4836. if (i < 0)
  4837. return i;
  4838. nfs4_inode_make_writeable(inode);
  4839. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4840. /*
  4841. * Free each page after tx, so the only ref left is
  4842. * held by the network stack
  4843. */
  4844. for (; i > 0; i--)
  4845. put_page(pages[i-1]);
  4846. /*
  4847. * Acl update can result in inode attribute update.
  4848. * so mark the attribute cache invalid.
  4849. */
  4850. spin_lock(&inode->i_lock);
  4851. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
  4852. | NFS_INO_INVALID_CTIME
  4853. | NFS_INO_REVAL_FORCED;
  4854. spin_unlock(&inode->i_lock);
  4855. nfs_access_zap_cache(inode);
  4856. nfs_zap_acl_cache(inode);
  4857. return ret;
  4858. }
  4859. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  4860. {
  4861. struct nfs4_exception exception = { };
  4862. int err;
  4863. do {
  4864. err = __nfs4_proc_set_acl(inode, buf, buflen);
  4865. trace_nfs4_set_acl(inode, err);
  4866. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4867. &exception);
  4868. } while (exception.retry);
  4869. return err;
  4870. }
  4871. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  4872. static int _nfs4_get_security_label(struct inode *inode, void *buf,
  4873. size_t buflen)
  4874. {
  4875. struct nfs_server *server = NFS_SERVER(inode);
  4876. struct nfs_fattr fattr;
  4877. struct nfs4_label label = {0, 0, buflen, buf};
  4878. u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  4879. struct nfs4_getattr_arg arg = {
  4880. .fh = NFS_FH(inode),
  4881. .bitmask = bitmask,
  4882. };
  4883. struct nfs4_getattr_res res = {
  4884. .fattr = &fattr,
  4885. .label = &label,
  4886. .server = server,
  4887. };
  4888. struct rpc_message msg = {
  4889. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  4890. .rpc_argp = &arg,
  4891. .rpc_resp = &res,
  4892. };
  4893. int ret;
  4894. nfs_fattr_init(&fattr);
  4895. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
  4896. if (ret)
  4897. return ret;
  4898. if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
  4899. return -ENOENT;
  4900. if (buflen < label.len)
  4901. return -ERANGE;
  4902. return 0;
  4903. }
  4904. static int nfs4_get_security_label(struct inode *inode, void *buf,
  4905. size_t buflen)
  4906. {
  4907. struct nfs4_exception exception = { };
  4908. int err;
  4909. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  4910. return -EOPNOTSUPP;
  4911. do {
  4912. err = _nfs4_get_security_label(inode, buf, buflen);
  4913. trace_nfs4_get_security_label(inode, err);
  4914. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4915. &exception);
  4916. } while (exception.retry);
  4917. return err;
  4918. }
  4919. static int _nfs4_do_set_security_label(struct inode *inode,
  4920. struct nfs4_label *ilabel,
  4921. struct nfs_fattr *fattr,
  4922. struct nfs4_label *olabel)
  4923. {
  4924. struct iattr sattr = {0};
  4925. struct nfs_server *server = NFS_SERVER(inode);
  4926. const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  4927. struct nfs_setattrargs arg = {
  4928. .fh = NFS_FH(inode),
  4929. .iap = &sattr,
  4930. .server = server,
  4931. .bitmask = bitmask,
  4932. .label = ilabel,
  4933. };
  4934. struct nfs_setattrres res = {
  4935. .fattr = fattr,
  4936. .label = olabel,
  4937. .server = server,
  4938. };
  4939. struct rpc_message msg = {
  4940. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  4941. .rpc_argp = &arg,
  4942. .rpc_resp = &res,
  4943. };
  4944. int status;
  4945. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  4946. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4947. if (status)
  4948. dprintk("%s failed: %d\n", __func__, status);
  4949. return status;
  4950. }
  4951. static int nfs4_do_set_security_label(struct inode *inode,
  4952. struct nfs4_label *ilabel,
  4953. struct nfs_fattr *fattr,
  4954. struct nfs4_label *olabel)
  4955. {
  4956. struct nfs4_exception exception = { };
  4957. int err;
  4958. do {
  4959. err = _nfs4_do_set_security_label(inode, ilabel,
  4960. fattr, olabel);
  4961. trace_nfs4_set_security_label(inode, err);
  4962. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4963. &exception);
  4964. } while (exception.retry);
  4965. return err;
  4966. }
  4967. static int
  4968. nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
  4969. {
  4970. struct nfs4_label ilabel, *olabel = NULL;
  4971. struct nfs_fattr fattr;
  4972. struct rpc_cred *cred;
  4973. int status;
  4974. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  4975. return -EOPNOTSUPP;
  4976. nfs_fattr_init(&fattr);
  4977. ilabel.pi = 0;
  4978. ilabel.lfs = 0;
  4979. ilabel.label = (char *)buf;
  4980. ilabel.len = buflen;
  4981. cred = rpc_lookup_cred();
  4982. if (IS_ERR(cred))
  4983. return PTR_ERR(cred);
  4984. olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  4985. if (IS_ERR(olabel)) {
  4986. status = -PTR_ERR(olabel);
  4987. goto out;
  4988. }
  4989. status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
  4990. if (status == 0)
  4991. nfs_setsecurity(inode, &fattr, olabel);
  4992. nfs4_label_free(olabel);
  4993. out:
  4994. put_rpccred(cred);
  4995. return status;
  4996. }
  4997. #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
  4998. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  4999. nfs4_verifier *bootverf)
  5000. {
  5001. __be32 verf[2];
  5002. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  5003. /* An impossible timestamp guarantees this value
  5004. * will never match a generated boot time. */
  5005. verf[0] = cpu_to_be32(U32_MAX);
  5006. verf[1] = cpu_to_be32(U32_MAX);
  5007. } else {
  5008. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  5009. u64 ns = ktime_to_ns(nn->boot_time);
  5010. verf[0] = cpu_to_be32(ns >> 32);
  5011. verf[1] = cpu_to_be32(ns);
  5012. }
  5013. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  5014. }
  5015. static int
  5016. nfs4_init_nonuniform_client_string(struct nfs_client *clp)
  5017. {
  5018. size_t len;
  5019. char *str;
  5020. if (clp->cl_owner_id != NULL)
  5021. return 0;
  5022. rcu_read_lock();
  5023. len = 14 + strlen(clp->cl_ipaddr) + 1 +
  5024. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
  5025. 1 +
  5026. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
  5027. 1;
  5028. rcu_read_unlock();
  5029. if (len > NFS4_OPAQUE_LIMIT + 1)
  5030. return -EINVAL;
  5031. /*
  5032. * Since this string is allocated at mount time, and held until the
  5033. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  5034. * about a memory-reclaim deadlock.
  5035. */
  5036. str = kmalloc(len, GFP_KERNEL);
  5037. if (!str)
  5038. return -ENOMEM;
  5039. rcu_read_lock();
  5040. scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
  5041. clp->cl_ipaddr,
  5042. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  5043. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
  5044. rcu_read_unlock();
  5045. clp->cl_owner_id = str;
  5046. return 0;
  5047. }
  5048. static int
  5049. nfs4_init_uniquifier_client_string(struct nfs_client *clp)
  5050. {
  5051. size_t len;
  5052. char *str;
  5053. len = 10 + 10 + 1 + 10 + 1 +
  5054. strlen(nfs4_client_id_uniquifier) + 1 +
  5055. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  5056. if (len > NFS4_OPAQUE_LIMIT + 1)
  5057. return -EINVAL;
  5058. /*
  5059. * Since this string is allocated at mount time, and held until the
  5060. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  5061. * about a memory-reclaim deadlock.
  5062. */
  5063. str = kmalloc(len, GFP_KERNEL);
  5064. if (!str)
  5065. return -ENOMEM;
  5066. scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
  5067. clp->rpc_ops->version, clp->cl_minorversion,
  5068. nfs4_client_id_uniquifier,
  5069. clp->cl_rpcclient->cl_nodename);
  5070. clp->cl_owner_id = str;
  5071. return 0;
  5072. }
  5073. static int
  5074. nfs4_init_uniform_client_string(struct nfs_client *clp)
  5075. {
  5076. size_t len;
  5077. char *str;
  5078. if (clp->cl_owner_id != NULL)
  5079. return 0;
  5080. if (nfs4_client_id_uniquifier[0] != '\0')
  5081. return nfs4_init_uniquifier_client_string(clp);
  5082. len = 10 + 10 + 1 + 10 + 1 +
  5083. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  5084. if (len > NFS4_OPAQUE_LIMIT + 1)
  5085. return -EINVAL;
  5086. /*
  5087. * Since this string is allocated at mount time, and held until the
  5088. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  5089. * about a memory-reclaim deadlock.
  5090. */
  5091. str = kmalloc(len, GFP_KERNEL);
  5092. if (!str)
  5093. return -ENOMEM;
  5094. scnprintf(str, len, "Linux NFSv%u.%u %s",
  5095. clp->rpc_ops->version, clp->cl_minorversion,
  5096. clp->cl_rpcclient->cl_nodename);
  5097. clp->cl_owner_id = str;
  5098. return 0;
  5099. }
  5100. /*
  5101. * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
  5102. * services. Advertise one based on the address family of the
  5103. * clientaddr.
  5104. */
  5105. static unsigned int
  5106. nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
  5107. {
  5108. if (strchr(clp->cl_ipaddr, ':') != NULL)
  5109. return scnprintf(buf, len, "tcp6");
  5110. else
  5111. return scnprintf(buf, len, "tcp");
  5112. }
  5113. static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
  5114. {
  5115. struct nfs4_setclientid *sc = calldata;
  5116. if (task->tk_status == 0)
  5117. sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
  5118. }
  5119. static const struct rpc_call_ops nfs4_setclientid_ops = {
  5120. .rpc_call_done = nfs4_setclientid_done,
  5121. };
  5122. /**
  5123. * nfs4_proc_setclientid - Negotiate client ID
  5124. * @clp: state data structure
  5125. * @program: RPC program for NFSv4 callback service
  5126. * @port: IP port number for NFS4 callback service
  5127. * @cred: RPC credential to use for this call
  5128. * @res: where to place the result
  5129. *
  5130. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  5131. */
  5132. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  5133. unsigned short port, struct rpc_cred *cred,
  5134. struct nfs4_setclientid_res *res)
  5135. {
  5136. nfs4_verifier sc_verifier;
  5137. struct nfs4_setclientid setclientid = {
  5138. .sc_verifier = &sc_verifier,
  5139. .sc_prog = program,
  5140. .sc_clnt = clp,
  5141. };
  5142. struct rpc_message msg = {
  5143. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  5144. .rpc_argp = &setclientid,
  5145. .rpc_resp = res,
  5146. .rpc_cred = cred,
  5147. };
  5148. struct rpc_task *task;
  5149. struct rpc_task_setup task_setup_data = {
  5150. .rpc_client = clp->cl_rpcclient,
  5151. .rpc_message = &msg,
  5152. .callback_ops = &nfs4_setclientid_ops,
  5153. .callback_data = &setclientid,
  5154. .flags = RPC_TASK_TIMEOUT,
  5155. };
  5156. int status;
  5157. /* nfs_client_id4 */
  5158. nfs4_init_boot_verifier(clp, &sc_verifier);
  5159. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  5160. status = nfs4_init_uniform_client_string(clp);
  5161. else
  5162. status = nfs4_init_nonuniform_client_string(clp);
  5163. if (status)
  5164. goto out;
  5165. /* cb_client4 */
  5166. setclientid.sc_netid_len =
  5167. nfs4_init_callback_netid(clp,
  5168. setclientid.sc_netid,
  5169. sizeof(setclientid.sc_netid));
  5170. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  5171. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  5172. clp->cl_ipaddr, port >> 8, port & 255);
  5173. dprintk("NFS call setclientid auth=%s, '%s'\n",
  5174. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  5175. clp->cl_owner_id);
  5176. task = rpc_run_task(&task_setup_data);
  5177. if (IS_ERR(task)) {
  5178. status = PTR_ERR(task);
  5179. goto out;
  5180. }
  5181. status = task->tk_status;
  5182. if (setclientid.sc_cred) {
  5183. clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
  5184. put_rpccred(setclientid.sc_cred);
  5185. }
  5186. rpc_put_task(task);
  5187. out:
  5188. trace_nfs4_setclientid(clp, status);
  5189. dprintk("NFS reply setclientid: %d\n", status);
  5190. return status;
  5191. }
  5192. /**
  5193. * nfs4_proc_setclientid_confirm - Confirm client ID
  5194. * @clp: state data structure
  5195. * @res: result of a previous SETCLIENTID
  5196. * @cred: RPC credential to use for this call
  5197. *
  5198. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  5199. */
  5200. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  5201. struct nfs4_setclientid_res *arg,
  5202. struct rpc_cred *cred)
  5203. {
  5204. struct rpc_message msg = {
  5205. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  5206. .rpc_argp = arg,
  5207. .rpc_cred = cred,
  5208. };
  5209. int status;
  5210. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  5211. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  5212. clp->cl_clientid);
  5213. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5214. trace_nfs4_setclientid_confirm(clp, status);
  5215. dprintk("NFS reply setclientid_confirm: %d\n", status);
  5216. return status;
  5217. }
  5218. struct nfs4_delegreturndata {
  5219. struct nfs4_delegreturnargs args;
  5220. struct nfs4_delegreturnres res;
  5221. struct nfs_fh fh;
  5222. nfs4_stateid stateid;
  5223. unsigned long timestamp;
  5224. struct {
  5225. struct nfs4_layoutreturn_args arg;
  5226. struct nfs4_layoutreturn_res res;
  5227. struct nfs4_xdr_opaque_data ld_private;
  5228. u32 roc_barrier;
  5229. bool roc;
  5230. } lr;
  5231. struct nfs_fattr fattr;
  5232. int rpc_status;
  5233. struct inode *inode;
  5234. };
  5235. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  5236. {
  5237. struct nfs4_delegreturndata *data = calldata;
  5238. struct nfs4_exception exception = {
  5239. .inode = data->inode,
  5240. .stateid = &data->stateid,
  5241. };
  5242. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5243. return;
  5244. trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
  5245. /* Handle Layoutreturn errors */
  5246. if (data->args.lr_args && task->tk_status != 0) {
  5247. switch(data->res.lr_ret) {
  5248. default:
  5249. data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  5250. break;
  5251. case 0:
  5252. data->args.lr_args = NULL;
  5253. data->res.lr_res = NULL;
  5254. break;
  5255. case -NFS4ERR_OLD_STATEID:
  5256. if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
  5257. data->inode))
  5258. goto lr_restart;
  5259. /* Fallthrough */
  5260. case -NFS4ERR_ADMIN_REVOKED:
  5261. case -NFS4ERR_DELEG_REVOKED:
  5262. case -NFS4ERR_EXPIRED:
  5263. case -NFS4ERR_BAD_STATEID:
  5264. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  5265. case -NFS4ERR_WRONG_CRED:
  5266. data->args.lr_args = NULL;
  5267. data->res.lr_res = NULL;
  5268. goto lr_restart;
  5269. }
  5270. }
  5271. switch (task->tk_status) {
  5272. case 0:
  5273. renew_lease(data->res.server, data->timestamp);
  5274. break;
  5275. case -NFS4ERR_ADMIN_REVOKED:
  5276. case -NFS4ERR_DELEG_REVOKED:
  5277. case -NFS4ERR_EXPIRED:
  5278. nfs4_free_revoked_stateid(data->res.server,
  5279. data->args.stateid,
  5280. task->tk_msg.rpc_cred);
  5281. /* Fallthrough */
  5282. case -NFS4ERR_BAD_STATEID:
  5283. case -NFS4ERR_STALE_STATEID:
  5284. task->tk_status = 0;
  5285. break;
  5286. case -NFS4ERR_OLD_STATEID:
  5287. if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
  5288. goto out_restart;
  5289. task->tk_status = 0;
  5290. break;
  5291. case -NFS4ERR_ACCESS:
  5292. if (data->args.bitmask) {
  5293. data->args.bitmask = NULL;
  5294. data->res.fattr = NULL;
  5295. goto out_restart;
  5296. }
  5297. /* Fallthrough */
  5298. default:
  5299. task->tk_status = nfs4_async_handle_exception(task,
  5300. data->res.server, task->tk_status,
  5301. &exception);
  5302. if (exception.retry)
  5303. goto out_restart;
  5304. }
  5305. data->rpc_status = task->tk_status;
  5306. return;
  5307. lr_restart:
  5308. data->res.lr_ret = 0;
  5309. out_restart:
  5310. task->tk_status = 0;
  5311. rpc_restart_call_prepare(task);
  5312. }
  5313. static void nfs4_delegreturn_release(void *calldata)
  5314. {
  5315. struct nfs4_delegreturndata *data = calldata;
  5316. struct inode *inode = data->inode;
  5317. if (inode) {
  5318. if (data->lr.roc)
  5319. pnfs_roc_release(&data->lr.arg, &data->lr.res,
  5320. data->res.lr_ret);
  5321. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  5322. nfs_iput_and_deactive(inode);
  5323. }
  5324. kfree(calldata);
  5325. }
  5326. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  5327. {
  5328. struct nfs4_delegreturndata *d_data;
  5329. d_data = (struct nfs4_delegreturndata *)data;
  5330. if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
  5331. return;
  5332. nfs4_setup_sequence(d_data->res.server->nfs_client,
  5333. &d_data->args.seq_args,
  5334. &d_data->res.seq_res,
  5335. task);
  5336. }
  5337. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  5338. .rpc_call_prepare = nfs4_delegreturn_prepare,
  5339. .rpc_call_done = nfs4_delegreturn_done,
  5340. .rpc_release = nfs4_delegreturn_release,
  5341. };
  5342. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  5343. {
  5344. struct nfs4_delegreturndata *data;
  5345. struct nfs_server *server = NFS_SERVER(inode);
  5346. struct rpc_task *task;
  5347. struct rpc_message msg = {
  5348. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  5349. .rpc_cred = cred,
  5350. };
  5351. struct rpc_task_setup task_setup_data = {
  5352. .rpc_client = server->client,
  5353. .rpc_message = &msg,
  5354. .callback_ops = &nfs4_delegreturn_ops,
  5355. .flags = RPC_TASK_ASYNC,
  5356. };
  5357. int status = 0;
  5358. data = kzalloc(sizeof(*data), GFP_NOFS);
  5359. if (data == NULL)
  5360. return -ENOMEM;
  5361. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
  5362. nfs4_state_protect(server->nfs_client,
  5363. NFS_SP4_MACH_CRED_CLEANUP,
  5364. &task_setup_data.rpc_client, &msg);
  5365. data->args.fhandle = &data->fh;
  5366. data->args.stateid = &data->stateid;
  5367. data->args.bitmask = server->cache_consistency_bitmask;
  5368. nfs_copy_fh(&data->fh, NFS_FH(inode));
  5369. nfs4_stateid_copy(&data->stateid, stateid);
  5370. data->res.fattr = &data->fattr;
  5371. data->res.server = server;
  5372. data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  5373. data->lr.arg.ld_private = &data->lr.ld_private;
  5374. nfs_fattr_init(data->res.fattr);
  5375. data->timestamp = jiffies;
  5376. data->rpc_status = 0;
  5377. data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
  5378. data->inode = nfs_igrab_and_active(inode);
  5379. if (data->inode) {
  5380. if (data->lr.roc) {
  5381. data->args.lr_args = &data->lr.arg;
  5382. data->res.lr_res = &data->lr.res;
  5383. }
  5384. } else if (data->lr.roc) {
  5385. pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
  5386. data->lr.roc = false;
  5387. }
  5388. task_setup_data.callback_data = data;
  5389. msg.rpc_argp = &data->args;
  5390. msg.rpc_resp = &data->res;
  5391. task = rpc_run_task(&task_setup_data);
  5392. if (IS_ERR(task))
  5393. return PTR_ERR(task);
  5394. if (!issync)
  5395. goto out;
  5396. status = rpc_wait_for_completion_task(task);
  5397. if (status != 0)
  5398. goto out;
  5399. status = data->rpc_status;
  5400. out:
  5401. rpc_put_task(task);
  5402. return status;
  5403. }
  5404. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  5405. {
  5406. struct nfs_server *server = NFS_SERVER(inode);
  5407. struct nfs4_exception exception = { };
  5408. int err;
  5409. do {
  5410. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  5411. trace_nfs4_delegreturn(inode, stateid, err);
  5412. switch (err) {
  5413. case -NFS4ERR_STALE_STATEID:
  5414. case -NFS4ERR_EXPIRED:
  5415. case 0:
  5416. return 0;
  5417. }
  5418. err = nfs4_handle_exception(server, err, &exception);
  5419. } while (exception.retry);
  5420. return err;
  5421. }
  5422. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5423. {
  5424. struct inode *inode = state->inode;
  5425. struct nfs_server *server = NFS_SERVER(inode);
  5426. struct nfs_client *clp = server->nfs_client;
  5427. struct nfs_lockt_args arg = {
  5428. .fh = NFS_FH(inode),
  5429. .fl = request,
  5430. };
  5431. struct nfs_lockt_res res = {
  5432. .denied = request,
  5433. };
  5434. struct rpc_message msg = {
  5435. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  5436. .rpc_argp = &arg,
  5437. .rpc_resp = &res,
  5438. .rpc_cred = state->owner->so_cred,
  5439. };
  5440. struct nfs4_lock_state *lsp;
  5441. int status;
  5442. arg.lock_owner.clientid = clp->cl_clientid;
  5443. status = nfs4_set_lock_state(state, request);
  5444. if (status != 0)
  5445. goto out;
  5446. lsp = request->fl_u.nfs4_fl.owner;
  5447. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  5448. arg.lock_owner.s_dev = server->s_dev;
  5449. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  5450. switch (status) {
  5451. case 0:
  5452. request->fl_type = F_UNLCK;
  5453. break;
  5454. case -NFS4ERR_DENIED:
  5455. status = 0;
  5456. }
  5457. request->fl_ops->fl_release_private(request);
  5458. request->fl_ops = NULL;
  5459. out:
  5460. return status;
  5461. }
  5462. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5463. {
  5464. struct nfs4_exception exception = { };
  5465. int err;
  5466. do {
  5467. err = _nfs4_proc_getlk(state, cmd, request);
  5468. trace_nfs4_get_lock(request, state, cmd, err);
  5469. err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
  5470. &exception);
  5471. } while (exception.retry);
  5472. return err;
  5473. }
  5474. struct nfs4_unlockdata {
  5475. struct nfs_locku_args arg;
  5476. struct nfs_locku_res res;
  5477. struct nfs4_lock_state *lsp;
  5478. struct nfs_open_context *ctx;
  5479. struct nfs_lock_context *l_ctx;
  5480. struct file_lock fl;
  5481. struct nfs_server *server;
  5482. unsigned long timestamp;
  5483. };
  5484. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  5485. struct nfs_open_context *ctx,
  5486. struct nfs4_lock_state *lsp,
  5487. struct nfs_seqid *seqid)
  5488. {
  5489. struct nfs4_unlockdata *p;
  5490. struct inode *inode = lsp->ls_state->inode;
  5491. p = kzalloc(sizeof(*p), GFP_NOFS);
  5492. if (p == NULL)
  5493. return NULL;
  5494. p->arg.fh = NFS_FH(inode);
  5495. p->arg.fl = &p->fl;
  5496. p->arg.seqid = seqid;
  5497. p->res.seqid = seqid;
  5498. p->lsp = lsp;
  5499. refcount_inc(&lsp->ls_count);
  5500. /* Ensure we don't close file until we're done freeing locks! */
  5501. p->ctx = get_nfs_open_context(ctx);
  5502. p->l_ctx = nfs_get_lock_context(ctx);
  5503. memcpy(&p->fl, fl, sizeof(p->fl));
  5504. p->server = NFS_SERVER(inode);
  5505. return p;
  5506. }
  5507. static void nfs4_locku_release_calldata(void *data)
  5508. {
  5509. struct nfs4_unlockdata *calldata = data;
  5510. nfs_free_seqid(calldata->arg.seqid);
  5511. nfs4_put_lock_state(calldata->lsp);
  5512. nfs_put_lock_context(calldata->l_ctx);
  5513. put_nfs_open_context(calldata->ctx);
  5514. kfree(calldata);
  5515. }
  5516. static void nfs4_locku_done(struct rpc_task *task, void *data)
  5517. {
  5518. struct nfs4_unlockdata *calldata = data;
  5519. struct nfs4_exception exception = {
  5520. .inode = calldata->lsp->ls_state->inode,
  5521. .stateid = &calldata->arg.stateid,
  5522. };
  5523. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  5524. return;
  5525. switch (task->tk_status) {
  5526. case 0:
  5527. renew_lease(calldata->server, calldata->timestamp);
  5528. locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
  5529. if (nfs4_update_lock_stateid(calldata->lsp,
  5530. &calldata->res.stateid))
  5531. break;
  5532. case -NFS4ERR_ADMIN_REVOKED:
  5533. case -NFS4ERR_EXPIRED:
  5534. nfs4_free_revoked_stateid(calldata->server,
  5535. &calldata->arg.stateid,
  5536. task->tk_msg.rpc_cred);
  5537. case -NFS4ERR_BAD_STATEID:
  5538. case -NFS4ERR_OLD_STATEID:
  5539. case -NFS4ERR_STALE_STATEID:
  5540. if (!nfs4_stateid_match(&calldata->arg.stateid,
  5541. &calldata->lsp->ls_stateid))
  5542. rpc_restart_call_prepare(task);
  5543. break;
  5544. default:
  5545. task->tk_status = nfs4_async_handle_exception(task,
  5546. calldata->server, task->tk_status,
  5547. &exception);
  5548. if (exception.retry)
  5549. rpc_restart_call_prepare(task);
  5550. }
  5551. nfs_release_seqid(calldata->arg.seqid);
  5552. }
  5553. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  5554. {
  5555. struct nfs4_unlockdata *calldata = data;
  5556. if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
  5557. nfs_async_iocounter_wait(task, calldata->l_ctx))
  5558. return;
  5559. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  5560. goto out_wait;
  5561. nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
  5562. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  5563. /* Note: exit _without_ running nfs4_locku_done */
  5564. goto out_no_action;
  5565. }
  5566. calldata->timestamp = jiffies;
  5567. if (nfs4_setup_sequence(calldata->server->nfs_client,
  5568. &calldata->arg.seq_args,
  5569. &calldata->res.seq_res,
  5570. task) != 0)
  5571. nfs_release_seqid(calldata->arg.seqid);
  5572. return;
  5573. out_no_action:
  5574. task->tk_action = NULL;
  5575. out_wait:
  5576. nfs4_sequence_done(task, &calldata->res.seq_res);
  5577. }
  5578. static const struct rpc_call_ops nfs4_locku_ops = {
  5579. .rpc_call_prepare = nfs4_locku_prepare,
  5580. .rpc_call_done = nfs4_locku_done,
  5581. .rpc_release = nfs4_locku_release_calldata,
  5582. };
  5583. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  5584. struct nfs_open_context *ctx,
  5585. struct nfs4_lock_state *lsp,
  5586. struct nfs_seqid *seqid)
  5587. {
  5588. struct nfs4_unlockdata *data;
  5589. struct rpc_message msg = {
  5590. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  5591. .rpc_cred = ctx->cred,
  5592. };
  5593. struct rpc_task_setup task_setup_data = {
  5594. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  5595. .rpc_message = &msg,
  5596. .callback_ops = &nfs4_locku_ops,
  5597. .workqueue = nfsiod_workqueue,
  5598. .flags = RPC_TASK_ASYNC,
  5599. };
  5600. nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
  5601. NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
  5602. /* Ensure this is an unlock - when canceling a lock, the
  5603. * canceled lock is passed in, and it won't be an unlock.
  5604. */
  5605. fl->fl_type = F_UNLCK;
  5606. if (fl->fl_flags & FL_CLOSE)
  5607. set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
  5608. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  5609. if (data == NULL) {
  5610. nfs_free_seqid(seqid);
  5611. return ERR_PTR(-ENOMEM);
  5612. }
  5613. nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
  5614. msg.rpc_argp = &data->arg;
  5615. msg.rpc_resp = &data->res;
  5616. task_setup_data.callback_data = data;
  5617. return rpc_run_task(&task_setup_data);
  5618. }
  5619. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  5620. {
  5621. struct inode *inode = state->inode;
  5622. struct nfs4_state_owner *sp = state->owner;
  5623. struct nfs_inode *nfsi = NFS_I(inode);
  5624. struct nfs_seqid *seqid;
  5625. struct nfs4_lock_state *lsp;
  5626. struct rpc_task *task;
  5627. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  5628. int status = 0;
  5629. unsigned char fl_flags = request->fl_flags;
  5630. status = nfs4_set_lock_state(state, request);
  5631. /* Unlock _before_ we do the RPC call */
  5632. request->fl_flags |= FL_EXISTS;
  5633. /* Exclude nfs_delegation_claim_locks() */
  5634. mutex_lock(&sp->so_delegreturn_mutex);
  5635. /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
  5636. down_read(&nfsi->rwsem);
  5637. if (locks_lock_inode_wait(inode, request) == -ENOENT) {
  5638. up_read(&nfsi->rwsem);
  5639. mutex_unlock(&sp->so_delegreturn_mutex);
  5640. goto out;
  5641. }
  5642. up_read(&nfsi->rwsem);
  5643. mutex_unlock(&sp->so_delegreturn_mutex);
  5644. if (status != 0)
  5645. goto out;
  5646. /* Is this a delegated lock? */
  5647. lsp = request->fl_u.nfs4_fl.owner;
  5648. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
  5649. goto out;
  5650. alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
  5651. seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  5652. status = -ENOMEM;
  5653. if (IS_ERR(seqid))
  5654. goto out;
  5655. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  5656. status = PTR_ERR(task);
  5657. if (IS_ERR(task))
  5658. goto out;
  5659. status = rpc_wait_for_completion_task(task);
  5660. rpc_put_task(task);
  5661. out:
  5662. request->fl_flags = fl_flags;
  5663. trace_nfs4_unlock(request, state, F_SETLK, status);
  5664. return status;
  5665. }
  5666. struct nfs4_lockdata {
  5667. struct nfs_lock_args arg;
  5668. struct nfs_lock_res res;
  5669. struct nfs4_lock_state *lsp;
  5670. struct nfs_open_context *ctx;
  5671. struct file_lock fl;
  5672. unsigned long timestamp;
  5673. int rpc_status;
  5674. int cancelled;
  5675. struct nfs_server *server;
  5676. };
  5677. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  5678. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  5679. gfp_t gfp_mask)
  5680. {
  5681. struct nfs4_lockdata *p;
  5682. struct inode *inode = lsp->ls_state->inode;
  5683. struct nfs_server *server = NFS_SERVER(inode);
  5684. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  5685. p = kzalloc(sizeof(*p), gfp_mask);
  5686. if (p == NULL)
  5687. return NULL;
  5688. p->arg.fh = NFS_FH(inode);
  5689. p->arg.fl = &p->fl;
  5690. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  5691. if (IS_ERR(p->arg.open_seqid))
  5692. goto out_free;
  5693. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  5694. p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
  5695. if (IS_ERR(p->arg.lock_seqid))
  5696. goto out_free_seqid;
  5697. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  5698. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  5699. p->arg.lock_owner.s_dev = server->s_dev;
  5700. p->res.lock_seqid = p->arg.lock_seqid;
  5701. p->lsp = lsp;
  5702. p->server = server;
  5703. refcount_inc(&lsp->ls_count);
  5704. p->ctx = get_nfs_open_context(ctx);
  5705. memcpy(&p->fl, fl, sizeof(p->fl));
  5706. return p;
  5707. out_free_seqid:
  5708. nfs_free_seqid(p->arg.open_seqid);
  5709. out_free:
  5710. kfree(p);
  5711. return NULL;
  5712. }
  5713. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  5714. {
  5715. struct nfs4_lockdata *data = calldata;
  5716. struct nfs4_state *state = data->lsp->ls_state;
  5717. dprintk("%s: begin!\n", __func__);
  5718. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  5719. goto out_wait;
  5720. /* Do we need to do an open_to_lock_owner? */
  5721. if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
  5722. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  5723. goto out_release_lock_seqid;
  5724. }
  5725. nfs4_stateid_copy(&data->arg.open_stateid,
  5726. &state->open_stateid);
  5727. data->arg.new_lock_owner = 1;
  5728. data->res.open_seqid = data->arg.open_seqid;
  5729. } else {
  5730. data->arg.new_lock_owner = 0;
  5731. nfs4_stateid_copy(&data->arg.lock_stateid,
  5732. &data->lsp->ls_stateid);
  5733. }
  5734. if (!nfs4_valid_open_stateid(state)) {
  5735. data->rpc_status = -EBADF;
  5736. task->tk_action = NULL;
  5737. goto out_release_open_seqid;
  5738. }
  5739. data->timestamp = jiffies;
  5740. if (nfs4_setup_sequence(data->server->nfs_client,
  5741. &data->arg.seq_args,
  5742. &data->res.seq_res,
  5743. task) == 0)
  5744. return;
  5745. out_release_open_seqid:
  5746. nfs_release_seqid(data->arg.open_seqid);
  5747. out_release_lock_seqid:
  5748. nfs_release_seqid(data->arg.lock_seqid);
  5749. out_wait:
  5750. nfs4_sequence_done(task, &data->res.seq_res);
  5751. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  5752. }
  5753. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  5754. {
  5755. struct nfs4_lockdata *data = calldata;
  5756. struct nfs4_lock_state *lsp = data->lsp;
  5757. dprintk("%s: begin!\n", __func__);
  5758. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5759. return;
  5760. data->rpc_status = task->tk_status;
  5761. switch (task->tk_status) {
  5762. case 0:
  5763. renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
  5764. data->timestamp);
  5765. if (data->arg.new_lock && !data->cancelled) {
  5766. data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
  5767. if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
  5768. break;
  5769. }
  5770. if (data->arg.new_lock_owner != 0) {
  5771. nfs_confirm_seqid(&lsp->ls_seqid, 0);
  5772. nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
  5773. set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  5774. goto out_done;
  5775. } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
  5776. goto out_done;
  5777. break;
  5778. case -NFS4ERR_BAD_STATEID:
  5779. case -NFS4ERR_OLD_STATEID:
  5780. case -NFS4ERR_STALE_STATEID:
  5781. case -NFS4ERR_EXPIRED:
  5782. if (data->arg.new_lock_owner != 0) {
  5783. if (nfs4_stateid_match(&data->arg.open_stateid,
  5784. &lsp->ls_state->open_stateid))
  5785. goto out_done;
  5786. } else if (nfs4_stateid_match(&data->arg.lock_stateid,
  5787. &lsp->ls_stateid))
  5788. goto out_done;
  5789. }
  5790. if (!data->cancelled)
  5791. rpc_restart_call_prepare(task);
  5792. out_done:
  5793. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  5794. }
  5795. static void nfs4_lock_release(void *calldata)
  5796. {
  5797. struct nfs4_lockdata *data = calldata;
  5798. dprintk("%s: begin!\n", __func__);
  5799. nfs_free_seqid(data->arg.open_seqid);
  5800. if (data->cancelled) {
  5801. struct rpc_task *task;
  5802. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  5803. data->arg.lock_seqid);
  5804. if (!IS_ERR(task))
  5805. rpc_put_task_async(task);
  5806. dprintk("%s: cancelling lock!\n", __func__);
  5807. } else
  5808. nfs_free_seqid(data->arg.lock_seqid);
  5809. nfs4_put_lock_state(data->lsp);
  5810. put_nfs_open_context(data->ctx);
  5811. kfree(data);
  5812. dprintk("%s: done!\n", __func__);
  5813. }
  5814. static const struct rpc_call_ops nfs4_lock_ops = {
  5815. .rpc_call_prepare = nfs4_lock_prepare,
  5816. .rpc_call_done = nfs4_lock_done,
  5817. .rpc_release = nfs4_lock_release,
  5818. };
  5819. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  5820. {
  5821. switch (error) {
  5822. case -NFS4ERR_ADMIN_REVOKED:
  5823. case -NFS4ERR_EXPIRED:
  5824. case -NFS4ERR_BAD_STATEID:
  5825. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  5826. if (new_lock_owner != 0 ||
  5827. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  5828. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  5829. break;
  5830. case -NFS4ERR_STALE_STATEID:
  5831. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  5832. nfs4_schedule_lease_recovery(server->nfs_client);
  5833. };
  5834. }
  5835. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  5836. {
  5837. struct nfs4_lockdata *data;
  5838. struct rpc_task *task;
  5839. struct rpc_message msg = {
  5840. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  5841. .rpc_cred = state->owner->so_cred,
  5842. };
  5843. struct rpc_task_setup task_setup_data = {
  5844. .rpc_client = NFS_CLIENT(state->inode),
  5845. .rpc_message = &msg,
  5846. .callback_ops = &nfs4_lock_ops,
  5847. .workqueue = nfsiod_workqueue,
  5848. .flags = RPC_TASK_ASYNC,
  5849. };
  5850. int ret;
  5851. dprintk("%s: begin!\n", __func__);
  5852. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  5853. fl->fl_u.nfs4_fl.owner,
  5854. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  5855. if (data == NULL)
  5856. return -ENOMEM;
  5857. if (IS_SETLKW(cmd))
  5858. data->arg.block = 1;
  5859. nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
  5860. recovery_type > NFS_LOCK_NEW);
  5861. msg.rpc_argp = &data->arg;
  5862. msg.rpc_resp = &data->res;
  5863. task_setup_data.callback_data = data;
  5864. if (recovery_type > NFS_LOCK_NEW) {
  5865. if (recovery_type == NFS_LOCK_RECLAIM)
  5866. data->arg.reclaim = NFS_LOCK_RECLAIM;
  5867. } else
  5868. data->arg.new_lock = 1;
  5869. task = rpc_run_task(&task_setup_data);
  5870. if (IS_ERR(task))
  5871. return PTR_ERR(task);
  5872. ret = rpc_wait_for_completion_task(task);
  5873. if (ret == 0) {
  5874. ret = data->rpc_status;
  5875. if (ret)
  5876. nfs4_handle_setlk_error(data->server, data->lsp,
  5877. data->arg.new_lock_owner, ret);
  5878. } else
  5879. data->cancelled = true;
  5880. rpc_put_task(task);
  5881. dprintk("%s: done, ret = %d!\n", __func__, ret);
  5882. trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
  5883. return ret;
  5884. }
  5885. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  5886. {
  5887. struct nfs_server *server = NFS_SERVER(state->inode);
  5888. struct nfs4_exception exception = {
  5889. .inode = state->inode,
  5890. };
  5891. int err;
  5892. do {
  5893. /* Cache the lock if possible... */
  5894. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  5895. return 0;
  5896. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  5897. if (err != -NFS4ERR_DELAY)
  5898. break;
  5899. nfs4_handle_exception(server, err, &exception);
  5900. } while (exception.retry);
  5901. return err;
  5902. }
  5903. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  5904. {
  5905. struct nfs_server *server = NFS_SERVER(state->inode);
  5906. struct nfs4_exception exception = {
  5907. .inode = state->inode,
  5908. };
  5909. int err;
  5910. err = nfs4_set_lock_state(state, request);
  5911. if (err != 0)
  5912. return err;
  5913. if (!recover_lost_locks) {
  5914. set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
  5915. return 0;
  5916. }
  5917. do {
  5918. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  5919. return 0;
  5920. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  5921. switch (err) {
  5922. default:
  5923. goto out;
  5924. case -NFS4ERR_GRACE:
  5925. case -NFS4ERR_DELAY:
  5926. nfs4_handle_exception(server, err, &exception);
  5927. err = 0;
  5928. }
  5929. } while (exception.retry);
  5930. out:
  5931. return err;
  5932. }
  5933. #if defined(CONFIG_NFS_V4_1)
  5934. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  5935. {
  5936. struct nfs4_lock_state *lsp;
  5937. int status;
  5938. status = nfs4_set_lock_state(state, request);
  5939. if (status != 0)
  5940. return status;
  5941. lsp = request->fl_u.nfs4_fl.owner;
  5942. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
  5943. test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
  5944. return 0;
  5945. return nfs4_lock_expired(state, request);
  5946. }
  5947. #endif
  5948. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5949. {
  5950. struct nfs_inode *nfsi = NFS_I(state->inode);
  5951. struct nfs4_state_owner *sp = state->owner;
  5952. unsigned char fl_flags = request->fl_flags;
  5953. int status;
  5954. request->fl_flags |= FL_ACCESS;
  5955. status = locks_lock_inode_wait(state->inode, request);
  5956. if (status < 0)
  5957. goto out;
  5958. mutex_lock(&sp->so_delegreturn_mutex);
  5959. down_read(&nfsi->rwsem);
  5960. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  5961. /* Yes: cache locks! */
  5962. /* ...but avoid races with delegation recall... */
  5963. request->fl_flags = fl_flags & ~FL_SLEEP;
  5964. status = locks_lock_inode_wait(state->inode, request);
  5965. up_read(&nfsi->rwsem);
  5966. mutex_unlock(&sp->so_delegreturn_mutex);
  5967. goto out;
  5968. }
  5969. up_read(&nfsi->rwsem);
  5970. mutex_unlock(&sp->so_delegreturn_mutex);
  5971. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  5972. out:
  5973. request->fl_flags = fl_flags;
  5974. return status;
  5975. }
  5976. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5977. {
  5978. struct nfs4_exception exception = {
  5979. .state = state,
  5980. .inode = state->inode,
  5981. };
  5982. int err;
  5983. do {
  5984. err = _nfs4_proc_setlk(state, cmd, request);
  5985. if (err == -NFS4ERR_DENIED)
  5986. err = -EAGAIN;
  5987. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  5988. err, &exception);
  5989. } while (exception.retry);
  5990. return err;
  5991. }
  5992. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  5993. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  5994. static int
  5995. nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
  5996. struct file_lock *request)
  5997. {
  5998. int status = -ERESTARTSYS;
  5999. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  6000. while(!signalled()) {
  6001. status = nfs4_proc_setlk(state, cmd, request);
  6002. if ((status != -EAGAIN) || IS_SETLK(cmd))
  6003. break;
  6004. freezable_schedule_timeout_interruptible(timeout);
  6005. timeout *= 2;
  6006. timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
  6007. status = -ERESTARTSYS;
  6008. }
  6009. return status;
  6010. }
  6011. #ifdef CONFIG_NFS_V4_1
  6012. struct nfs4_lock_waiter {
  6013. struct task_struct *task;
  6014. struct inode *inode;
  6015. struct nfs_lowner *owner;
  6016. bool notified;
  6017. };
  6018. static int
  6019. nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
  6020. {
  6021. int ret;
  6022. struct nfs4_lock_waiter *waiter = wait->private;
  6023. /* NULL key means to wake up everyone */
  6024. if (key) {
  6025. struct cb_notify_lock_args *cbnl = key;
  6026. struct nfs_lowner *lowner = &cbnl->cbnl_owner,
  6027. *wowner = waiter->owner;
  6028. /* Only wake if the callback was for the same owner. */
  6029. if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
  6030. return 0;
  6031. /* Make sure it's for the right inode */
  6032. if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
  6033. return 0;
  6034. waiter->notified = true;
  6035. }
  6036. /* override "private" so we can use default_wake_function */
  6037. wait->private = waiter->task;
  6038. ret = autoremove_wake_function(wait, mode, flags, key);
  6039. wait->private = waiter;
  6040. return ret;
  6041. }
  6042. static int
  6043. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6044. {
  6045. int status = -ERESTARTSYS;
  6046. unsigned long flags;
  6047. struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
  6048. struct nfs_server *server = NFS_SERVER(state->inode);
  6049. struct nfs_client *clp = server->nfs_client;
  6050. wait_queue_head_t *q = &clp->cl_lock_waitq;
  6051. struct nfs_lowner owner = { .clientid = clp->cl_clientid,
  6052. .id = lsp->ls_seqid.owner_id,
  6053. .s_dev = server->s_dev };
  6054. struct nfs4_lock_waiter waiter = { .task = current,
  6055. .inode = state->inode,
  6056. .owner = &owner,
  6057. .notified = false };
  6058. wait_queue_entry_t wait;
  6059. /* Don't bother with waitqueue if we don't expect a callback */
  6060. if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
  6061. return nfs4_retry_setlk_simple(state, cmd, request);
  6062. init_wait(&wait);
  6063. wait.private = &waiter;
  6064. wait.func = nfs4_wake_lock_waiter;
  6065. add_wait_queue(q, &wait);
  6066. while(!signalled()) {
  6067. waiter.notified = false;
  6068. status = nfs4_proc_setlk(state, cmd, request);
  6069. if ((status != -EAGAIN) || IS_SETLK(cmd))
  6070. break;
  6071. status = -ERESTARTSYS;
  6072. spin_lock_irqsave(&q->lock, flags);
  6073. if (waiter.notified) {
  6074. spin_unlock_irqrestore(&q->lock, flags);
  6075. continue;
  6076. }
  6077. set_current_state(TASK_INTERRUPTIBLE);
  6078. spin_unlock_irqrestore(&q->lock, flags);
  6079. freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
  6080. }
  6081. finish_wait(q, &wait);
  6082. return status;
  6083. }
  6084. #else /* !CONFIG_NFS_V4_1 */
  6085. static inline int
  6086. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6087. {
  6088. return nfs4_retry_setlk_simple(state, cmd, request);
  6089. }
  6090. #endif
  6091. static int
  6092. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  6093. {
  6094. struct nfs_open_context *ctx;
  6095. struct nfs4_state *state;
  6096. int status;
  6097. /* verify open state */
  6098. ctx = nfs_file_open_context(filp);
  6099. state = ctx->state;
  6100. if (IS_GETLK(cmd)) {
  6101. if (state != NULL)
  6102. return nfs4_proc_getlk(state, F_GETLK, request);
  6103. return 0;
  6104. }
  6105. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  6106. return -EINVAL;
  6107. if (request->fl_type == F_UNLCK) {
  6108. if (state != NULL)
  6109. return nfs4_proc_unlck(state, cmd, request);
  6110. return 0;
  6111. }
  6112. if (state == NULL)
  6113. return -ENOLCK;
  6114. if ((request->fl_flags & FL_POSIX) &&
  6115. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  6116. return -ENOLCK;
  6117. /*
  6118. * Don't rely on the VFS having checked the file open mode,
  6119. * since it won't do this for flock() locks.
  6120. */
  6121. switch (request->fl_type) {
  6122. case F_RDLCK:
  6123. if (!(filp->f_mode & FMODE_READ))
  6124. return -EBADF;
  6125. break;
  6126. case F_WRLCK:
  6127. if (!(filp->f_mode & FMODE_WRITE))
  6128. return -EBADF;
  6129. }
  6130. status = nfs4_set_lock_state(state, request);
  6131. if (status != 0)
  6132. return status;
  6133. return nfs4_retry_setlk(state, cmd, request);
  6134. }
  6135. int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
  6136. {
  6137. struct nfs_server *server = NFS_SERVER(state->inode);
  6138. int err;
  6139. err = nfs4_set_lock_state(state, fl);
  6140. if (err != 0)
  6141. return err;
  6142. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  6143. return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
  6144. }
  6145. struct nfs_release_lockowner_data {
  6146. struct nfs4_lock_state *lsp;
  6147. struct nfs_server *server;
  6148. struct nfs_release_lockowner_args args;
  6149. struct nfs_release_lockowner_res res;
  6150. unsigned long timestamp;
  6151. };
  6152. static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
  6153. {
  6154. struct nfs_release_lockowner_data *data = calldata;
  6155. struct nfs_server *server = data->server;
  6156. nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
  6157. &data->res.seq_res, task);
  6158. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  6159. data->timestamp = jiffies;
  6160. }
  6161. static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
  6162. {
  6163. struct nfs_release_lockowner_data *data = calldata;
  6164. struct nfs_server *server = data->server;
  6165. nfs40_sequence_done(task, &data->res.seq_res);
  6166. switch (task->tk_status) {
  6167. case 0:
  6168. renew_lease(server, data->timestamp);
  6169. break;
  6170. case -NFS4ERR_STALE_CLIENTID:
  6171. case -NFS4ERR_EXPIRED:
  6172. nfs4_schedule_lease_recovery(server->nfs_client);
  6173. break;
  6174. case -NFS4ERR_LEASE_MOVED:
  6175. case -NFS4ERR_DELAY:
  6176. if (nfs4_async_handle_error(task, server,
  6177. NULL, NULL) == -EAGAIN)
  6178. rpc_restart_call_prepare(task);
  6179. }
  6180. }
  6181. static void nfs4_release_lockowner_release(void *calldata)
  6182. {
  6183. struct nfs_release_lockowner_data *data = calldata;
  6184. nfs4_free_lock_state(data->server, data->lsp);
  6185. kfree(calldata);
  6186. }
  6187. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  6188. .rpc_call_prepare = nfs4_release_lockowner_prepare,
  6189. .rpc_call_done = nfs4_release_lockowner_done,
  6190. .rpc_release = nfs4_release_lockowner_release,
  6191. };
  6192. static void
  6193. nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
  6194. {
  6195. struct nfs_release_lockowner_data *data;
  6196. struct rpc_message msg = {
  6197. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  6198. };
  6199. if (server->nfs_client->cl_mvops->minor_version != 0)
  6200. return;
  6201. data = kmalloc(sizeof(*data), GFP_NOFS);
  6202. if (!data)
  6203. return;
  6204. data->lsp = lsp;
  6205. data->server = server;
  6206. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  6207. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  6208. data->args.lock_owner.s_dev = server->s_dev;
  6209. msg.rpc_argp = &data->args;
  6210. msg.rpc_resp = &data->res;
  6211. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
  6212. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  6213. }
  6214. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  6215. static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
  6216. struct dentry *unused, struct inode *inode,
  6217. const char *key, const void *buf,
  6218. size_t buflen, int flags)
  6219. {
  6220. return nfs4_proc_set_acl(inode, buf, buflen);
  6221. }
  6222. static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
  6223. struct dentry *unused, struct inode *inode,
  6224. const char *key, void *buf, size_t buflen)
  6225. {
  6226. return nfs4_proc_get_acl(inode, buf, buflen);
  6227. }
  6228. static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
  6229. {
  6230. return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
  6231. }
  6232. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  6233. static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
  6234. struct dentry *unused, struct inode *inode,
  6235. const char *key, const void *buf,
  6236. size_t buflen, int flags)
  6237. {
  6238. if (security_ismaclabel(key))
  6239. return nfs4_set_security_label(inode, buf, buflen);
  6240. return -EOPNOTSUPP;
  6241. }
  6242. static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
  6243. struct dentry *unused, struct inode *inode,
  6244. const char *key, void *buf, size_t buflen)
  6245. {
  6246. if (security_ismaclabel(key))
  6247. return nfs4_get_security_label(inode, buf, buflen);
  6248. return -EOPNOTSUPP;
  6249. }
  6250. static ssize_t
  6251. nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
  6252. {
  6253. int len = 0;
  6254. if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
  6255. len = security_inode_listsecurity(inode, list, list_len);
  6256. if (list_len && len > list_len)
  6257. return -ERANGE;
  6258. }
  6259. return len;
  6260. }
  6261. static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
  6262. .prefix = XATTR_SECURITY_PREFIX,
  6263. .get = nfs4_xattr_get_nfs4_label,
  6264. .set = nfs4_xattr_set_nfs4_label,
  6265. };
  6266. #else
  6267. static ssize_t
  6268. nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
  6269. {
  6270. return 0;
  6271. }
  6272. #endif
  6273. /*
  6274. * nfs_fhget will use either the mounted_on_fileid or the fileid
  6275. */
  6276. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  6277. {
  6278. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  6279. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  6280. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  6281. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  6282. return;
  6283. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  6284. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  6285. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  6286. fattr->nlink = 2;
  6287. }
  6288. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  6289. const struct qstr *name,
  6290. struct nfs4_fs_locations *fs_locations,
  6291. struct page *page)
  6292. {
  6293. struct nfs_server *server = NFS_SERVER(dir);
  6294. u32 bitmask[3];
  6295. struct nfs4_fs_locations_arg args = {
  6296. .dir_fh = NFS_FH(dir),
  6297. .name = name,
  6298. .page = page,
  6299. .bitmask = bitmask,
  6300. };
  6301. struct nfs4_fs_locations_res res = {
  6302. .fs_locations = fs_locations,
  6303. };
  6304. struct rpc_message msg = {
  6305. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6306. .rpc_argp = &args,
  6307. .rpc_resp = &res,
  6308. };
  6309. int status;
  6310. dprintk("%s: start\n", __func__);
  6311. bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
  6312. bitmask[1] = nfs4_fattr_bitmap[1];
  6313. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  6314. * is not supported */
  6315. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  6316. bitmask[0] &= ~FATTR4_WORD0_FILEID;
  6317. else
  6318. bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
  6319. nfs_fattr_init(&fs_locations->fattr);
  6320. fs_locations->server = server;
  6321. fs_locations->nlocations = 0;
  6322. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  6323. dprintk("%s: returned status = %d\n", __func__, status);
  6324. return status;
  6325. }
  6326. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  6327. const struct qstr *name,
  6328. struct nfs4_fs_locations *fs_locations,
  6329. struct page *page)
  6330. {
  6331. struct nfs4_exception exception = { };
  6332. int err;
  6333. do {
  6334. err = _nfs4_proc_fs_locations(client, dir, name,
  6335. fs_locations, page);
  6336. trace_nfs4_get_fs_locations(dir, name, err);
  6337. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  6338. &exception);
  6339. } while (exception.retry);
  6340. return err;
  6341. }
  6342. /*
  6343. * This operation also signals the server that this client is
  6344. * performing migration recovery. The server can stop returning
  6345. * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
  6346. * appended to this compound to identify the client ID which is
  6347. * performing recovery.
  6348. */
  6349. static int _nfs40_proc_get_locations(struct inode *inode,
  6350. struct nfs4_fs_locations *locations,
  6351. struct page *page, struct rpc_cred *cred)
  6352. {
  6353. struct nfs_server *server = NFS_SERVER(inode);
  6354. struct rpc_clnt *clnt = server->client;
  6355. u32 bitmask[2] = {
  6356. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6357. };
  6358. struct nfs4_fs_locations_arg args = {
  6359. .clientid = server->nfs_client->cl_clientid,
  6360. .fh = NFS_FH(inode),
  6361. .page = page,
  6362. .bitmask = bitmask,
  6363. .migration = 1, /* skip LOOKUP */
  6364. .renew = 1, /* append RENEW */
  6365. };
  6366. struct nfs4_fs_locations_res res = {
  6367. .fs_locations = locations,
  6368. .migration = 1,
  6369. .renew = 1,
  6370. };
  6371. struct rpc_message msg = {
  6372. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6373. .rpc_argp = &args,
  6374. .rpc_resp = &res,
  6375. .rpc_cred = cred,
  6376. };
  6377. unsigned long now = jiffies;
  6378. int status;
  6379. nfs_fattr_init(&locations->fattr);
  6380. locations->server = server;
  6381. locations->nlocations = 0;
  6382. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
  6383. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6384. &args.seq_args, &res.seq_res);
  6385. if (status)
  6386. return status;
  6387. renew_lease(server, now);
  6388. return 0;
  6389. }
  6390. #ifdef CONFIG_NFS_V4_1
  6391. /*
  6392. * This operation also signals the server that this client is
  6393. * performing migration recovery. The server can stop asserting
  6394. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
  6395. * performing this operation is identified in the SEQUENCE
  6396. * operation in this compound.
  6397. *
  6398. * When the client supports GETATTR(fs_locations_info), it can
  6399. * be plumbed in here.
  6400. */
  6401. static int _nfs41_proc_get_locations(struct inode *inode,
  6402. struct nfs4_fs_locations *locations,
  6403. struct page *page, struct rpc_cred *cred)
  6404. {
  6405. struct nfs_server *server = NFS_SERVER(inode);
  6406. struct rpc_clnt *clnt = server->client;
  6407. u32 bitmask[2] = {
  6408. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6409. };
  6410. struct nfs4_fs_locations_arg args = {
  6411. .fh = NFS_FH(inode),
  6412. .page = page,
  6413. .bitmask = bitmask,
  6414. .migration = 1, /* skip LOOKUP */
  6415. };
  6416. struct nfs4_fs_locations_res res = {
  6417. .fs_locations = locations,
  6418. .migration = 1,
  6419. };
  6420. struct rpc_message msg = {
  6421. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6422. .rpc_argp = &args,
  6423. .rpc_resp = &res,
  6424. .rpc_cred = cred,
  6425. };
  6426. int status;
  6427. nfs_fattr_init(&locations->fattr);
  6428. locations->server = server;
  6429. locations->nlocations = 0;
  6430. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
  6431. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6432. &args.seq_args, &res.seq_res);
  6433. if (status == NFS4_OK &&
  6434. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  6435. status = -NFS4ERR_LEASE_MOVED;
  6436. return status;
  6437. }
  6438. #endif /* CONFIG_NFS_V4_1 */
  6439. /**
  6440. * nfs4_proc_get_locations - discover locations for a migrated FSID
  6441. * @inode: inode on FSID that is migrating
  6442. * @locations: result of query
  6443. * @page: buffer
  6444. * @cred: credential to use for this operation
  6445. *
  6446. * Returns NFS4_OK on success, a negative NFS4ERR status code if the
  6447. * operation failed, or a negative errno if a local error occurred.
  6448. *
  6449. * On success, "locations" is filled in, but if the server has
  6450. * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
  6451. * asserted.
  6452. *
  6453. * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
  6454. * from this client that require migration recovery.
  6455. */
  6456. int nfs4_proc_get_locations(struct inode *inode,
  6457. struct nfs4_fs_locations *locations,
  6458. struct page *page, struct rpc_cred *cred)
  6459. {
  6460. struct nfs_server *server = NFS_SERVER(inode);
  6461. struct nfs_client *clp = server->nfs_client;
  6462. const struct nfs4_mig_recovery_ops *ops =
  6463. clp->cl_mvops->mig_recovery_ops;
  6464. struct nfs4_exception exception = { };
  6465. int status;
  6466. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  6467. (unsigned long long)server->fsid.major,
  6468. (unsigned long long)server->fsid.minor,
  6469. clp->cl_hostname);
  6470. nfs_display_fhandle(NFS_FH(inode), __func__);
  6471. do {
  6472. status = ops->get_locations(inode, locations, page, cred);
  6473. if (status != -NFS4ERR_DELAY)
  6474. break;
  6475. nfs4_handle_exception(server, status, &exception);
  6476. } while (exception.retry);
  6477. return status;
  6478. }
  6479. /*
  6480. * This operation also signals the server that this client is
  6481. * performing "lease moved" recovery. The server can stop
  6482. * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
  6483. * is appended to this compound to identify the client ID which is
  6484. * performing recovery.
  6485. */
  6486. static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6487. {
  6488. struct nfs_server *server = NFS_SERVER(inode);
  6489. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  6490. struct rpc_clnt *clnt = server->client;
  6491. struct nfs4_fsid_present_arg args = {
  6492. .fh = NFS_FH(inode),
  6493. .clientid = clp->cl_clientid,
  6494. .renew = 1, /* append RENEW */
  6495. };
  6496. struct nfs4_fsid_present_res res = {
  6497. .renew = 1,
  6498. };
  6499. struct rpc_message msg = {
  6500. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  6501. .rpc_argp = &args,
  6502. .rpc_resp = &res,
  6503. .rpc_cred = cred,
  6504. };
  6505. unsigned long now = jiffies;
  6506. int status;
  6507. res.fh = nfs_alloc_fhandle();
  6508. if (res.fh == NULL)
  6509. return -ENOMEM;
  6510. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
  6511. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6512. &args.seq_args, &res.seq_res);
  6513. nfs_free_fhandle(res.fh);
  6514. if (status)
  6515. return status;
  6516. do_renew_lease(clp, now);
  6517. return 0;
  6518. }
  6519. #ifdef CONFIG_NFS_V4_1
  6520. /*
  6521. * This operation also signals the server that this client is
  6522. * performing "lease moved" recovery. The server can stop asserting
  6523. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
  6524. * this operation is identified in the SEQUENCE operation in this
  6525. * compound.
  6526. */
  6527. static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6528. {
  6529. struct nfs_server *server = NFS_SERVER(inode);
  6530. struct rpc_clnt *clnt = server->client;
  6531. struct nfs4_fsid_present_arg args = {
  6532. .fh = NFS_FH(inode),
  6533. };
  6534. struct nfs4_fsid_present_res res = {
  6535. };
  6536. struct rpc_message msg = {
  6537. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  6538. .rpc_argp = &args,
  6539. .rpc_resp = &res,
  6540. .rpc_cred = cred,
  6541. };
  6542. int status;
  6543. res.fh = nfs_alloc_fhandle();
  6544. if (res.fh == NULL)
  6545. return -ENOMEM;
  6546. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
  6547. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6548. &args.seq_args, &res.seq_res);
  6549. nfs_free_fhandle(res.fh);
  6550. if (status == NFS4_OK &&
  6551. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  6552. status = -NFS4ERR_LEASE_MOVED;
  6553. return status;
  6554. }
  6555. #endif /* CONFIG_NFS_V4_1 */
  6556. /**
  6557. * nfs4_proc_fsid_present - Is this FSID present or absent on server?
  6558. * @inode: inode on FSID to check
  6559. * @cred: credential to use for this operation
  6560. *
  6561. * Server indicates whether the FSID is present, moved, or not
  6562. * recognized. This operation is necessary to clear a LEASE_MOVED
  6563. * condition for this client ID.
  6564. *
  6565. * Returns NFS4_OK if the FSID is present on this server,
  6566. * -NFS4ERR_MOVED if the FSID is no longer present, a negative
  6567. * NFS4ERR code if some error occurred on the server, or a
  6568. * negative errno if a local failure occurred.
  6569. */
  6570. int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6571. {
  6572. struct nfs_server *server = NFS_SERVER(inode);
  6573. struct nfs_client *clp = server->nfs_client;
  6574. const struct nfs4_mig_recovery_ops *ops =
  6575. clp->cl_mvops->mig_recovery_ops;
  6576. struct nfs4_exception exception = { };
  6577. int status;
  6578. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  6579. (unsigned long long)server->fsid.major,
  6580. (unsigned long long)server->fsid.minor,
  6581. clp->cl_hostname);
  6582. nfs_display_fhandle(NFS_FH(inode), __func__);
  6583. do {
  6584. status = ops->fsid_present(inode, cred);
  6585. if (status != -NFS4ERR_DELAY)
  6586. break;
  6587. nfs4_handle_exception(server, status, &exception);
  6588. } while (exception.retry);
  6589. return status;
  6590. }
  6591. /**
  6592. * If 'use_integrity' is true and the state managment nfs_client
  6593. * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
  6594. * and the machine credential as per RFC3530bis and RFC5661 Security
  6595. * Considerations sections. Otherwise, just use the user cred with the
  6596. * filesystem's rpc_client.
  6597. */
  6598. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  6599. {
  6600. int status;
  6601. struct nfs4_secinfo_arg args = {
  6602. .dir_fh = NFS_FH(dir),
  6603. .name = name,
  6604. };
  6605. struct nfs4_secinfo_res res = {
  6606. .flavors = flavors,
  6607. };
  6608. struct rpc_message msg = {
  6609. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  6610. .rpc_argp = &args,
  6611. .rpc_resp = &res,
  6612. };
  6613. struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
  6614. struct rpc_cred *cred = NULL;
  6615. if (use_integrity) {
  6616. clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
  6617. cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
  6618. msg.rpc_cred = cred;
  6619. }
  6620. dprintk("NFS call secinfo %s\n", name->name);
  6621. nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
  6622. NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
  6623. status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
  6624. &res.seq_res, 0);
  6625. dprintk("NFS reply secinfo: %d\n", status);
  6626. if (cred)
  6627. put_rpccred(cred);
  6628. return status;
  6629. }
  6630. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  6631. struct nfs4_secinfo_flavors *flavors)
  6632. {
  6633. struct nfs4_exception exception = { };
  6634. int err;
  6635. do {
  6636. err = -NFS4ERR_WRONGSEC;
  6637. /* try to use integrity protection with machine cred */
  6638. if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
  6639. err = _nfs4_proc_secinfo(dir, name, flavors, true);
  6640. /*
  6641. * if unable to use integrity protection, or SECINFO with
  6642. * integrity protection returns NFS4ERR_WRONGSEC (which is
  6643. * disallowed by spec, but exists in deployed servers) use
  6644. * the current filesystem's rpc_client and the user cred.
  6645. */
  6646. if (err == -NFS4ERR_WRONGSEC)
  6647. err = _nfs4_proc_secinfo(dir, name, flavors, false);
  6648. trace_nfs4_secinfo(dir, name, err);
  6649. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  6650. &exception);
  6651. } while (exception.retry);
  6652. return err;
  6653. }
  6654. #ifdef CONFIG_NFS_V4_1
  6655. /*
  6656. * Check the exchange flags returned by the server for invalid flags, having
  6657. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  6658. * DS flags set.
  6659. */
  6660. static int nfs4_check_cl_exchange_flags(u32 flags)
  6661. {
  6662. if (flags & ~EXCHGID4_FLAG_MASK_R)
  6663. goto out_inval;
  6664. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  6665. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  6666. goto out_inval;
  6667. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  6668. goto out_inval;
  6669. return NFS_OK;
  6670. out_inval:
  6671. return -NFS4ERR_INVAL;
  6672. }
  6673. static bool
  6674. nfs41_same_server_scope(struct nfs41_server_scope *a,
  6675. struct nfs41_server_scope *b)
  6676. {
  6677. if (a->server_scope_sz != b->server_scope_sz)
  6678. return false;
  6679. return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
  6680. }
  6681. static void
  6682. nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
  6683. {
  6684. }
  6685. static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
  6686. .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
  6687. };
  6688. /*
  6689. * nfs4_proc_bind_one_conn_to_session()
  6690. *
  6691. * The 4.1 client currently uses the same TCP connection for the
  6692. * fore and backchannel.
  6693. */
  6694. static
  6695. int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
  6696. struct rpc_xprt *xprt,
  6697. struct nfs_client *clp,
  6698. struct rpc_cred *cred)
  6699. {
  6700. int status;
  6701. struct nfs41_bind_conn_to_session_args args = {
  6702. .client = clp,
  6703. .dir = NFS4_CDFC4_FORE_OR_BOTH,
  6704. };
  6705. struct nfs41_bind_conn_to_session_res res;
  6706. struct rpc_message msg = {
  6707. .rpc_proc =
  6708. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  6709. .rpc_argp = &args,
  6710. .rpc_resp = &res,
  6711. .rpc_cred = cred,
  6712. };
  6713. struct rpc_task_setup task_setup_data = {
  6714. .rpc_client = clnt,
  6715. .rpc_xprt = xprt,
  6716. .callback_ops = &nfs4_bind_one_conn_to_session_ops,
  6717. .rpc_message = &msg,
  6718. .flags = RPC_TASK_TIMEOUT,
  6719. };
  6720. struct rpc_task *task;
  6721. nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
  6722. if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
  6723. args.dir = NFS4_CDFC4_FORE;
  6724. /* Do not set the backchannel flag unless this is clnt->cl_xprt */
  6725. if (xprt != rcu_access_pointer(clnt->cl_xprt))
  6726. args.dir = NFS4_CDFC4_FORE;
  6727. task = rpc_run_task(&task_setup_data);
  6728. if (!IS_ERR(task)) {
  6729. status = task->tk_status;
  6730. rpc_put_task(task);
  6731. } else
  6732. status = PTR_ERR(task);
  6733. trace_nfs4_bind_conn_to_session(clp, status);
  6734. if (status == 0) {
  6735. if (memcmp(res.sessionid.data,
  6736. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  6737. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  6738. return -EIO;
  6739. }
  6740. if ((res.dir & args.dir) != res.dir || res.dir == 0) {
  6741. dprintk("NFS: %s: Unexpected direction from server\n",
  6742. __func__);
  6743. return -EIO;
  6744. }
  6745. if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
  6746. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  6747. __func__);
  6748. return -EIO;
  6749. }
  6750. }
  6751. return status;
  6752. }
  6753. struct rpc_bind_conn_calldata {
  6754. struct nfs_client *clp;
  6755. struct rpc_cred *cred;
  6756. };
  6757. static int
  6758. nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
  6759. struct rpc_xprt *xprt,
  6760. void *calldata)
  6761. {
  6762. struct rpc_bind_conn_calldata *p = calldata;
  6763. return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
  6764. }
  6765. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
  6766. {
  6767. struct rpc_bind_conn_calldata data = {
  6768. .clp = clp,
  6769. .cred = cred,
  6770. };
  6771. return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
  6772. nfs4_proc_bind_conn_to_session_callback, &data);
  6773. }
  6774. /*
  6775. * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
  6776. * and operations we'd like to see to enable certain features in the allow map
  6777. */
  6778. static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
  6779. .how = SP4_MACH_CRED,
  6780. .enforce.u.words = {
  6781. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  6782. 1 << (OP_EXCHANGE_ID - 32) |
  6783. 1 << (OP_CREATE_SESSION - 32) |
  6784. 1 << (OP_DESTROY_SESSION - 32) |
  6785. 1 << (OP_DESTROY_CLIENTID - 32)
  6786. },
  6787. .allow.u.words = {
  6788. [0] = 1 << (OP_CLOSE) |
  6789. 1 << (OP_OPEN_DOWNGRADE) |
  6790. 1 << (OP_LOCKU) |
  6791. 1 << (OP_DELEGRETURN) |
  6792. 1 << (OP_COMMIT),
  6793. [1] = 1 << (OP_SECINFO - 32) |
  6794. 1 << (OP_SECINFO_NO_NAME - 32) |
  6795. 1 << (OP_LAYOUTRETURN - 32) |
  6796. 1 << (OP_TEST_STATEID - 32) |
  6797. 1 << (OP_FREE_STATEID - 32) |
  6798. 1 << (OP_WRITE - 32)
  6799. }
  6800. };
  6801. /*
  6802. * Select the state protection mode for client `clp' given the server results
  6803. * from exchange_id in `sp'.
  6804. *
  6805. * Returns 0 on success, negative errno otherwise.
  6806. */
  6807. static int nfs4_sp4_select_mode(struct nfs_client *clp,
  6808. struct nfs41_state_protection *sp)
  6809. {
  6810. static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
  6811. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  6812. 1 << (OP_EXCHANGE_ID - 32) |
  6813. 1 << (OP_CREATE_SESSION - 32) |
  6814. 1 << (OP_DESTROY_SESSION - 32) |
  6815. 1 << (OP_DESTROY_CLIENTID - 32)
  6816. };
  6817. unsigned long flags = 0;
  6818. unsigned int i;
  6819. int ret = 0;
  6820. if (sp->how == SP4_MACH_CRED) {
  6821. /* Print state protect result */
  6822. dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
  6823. for (i = 0; i <= LAST_NFS4_OP; i++) {
  6824. if (test_bit(i, sp->enforce.u.longs))
  6825. dfprintk(MOUNT, " enforce op %d\n", i);
  6826. if (test_bit(i, sp->allow.u.longs))
  6827. dfprintk(MOUNT, " allow op %d\n", i);
  6828. }
  6829. /* make sure nothing is on enforce list that isn't supported */
  6830. for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
  6831. if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
  6832. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  6833. ret = -EINVAL;
  6834. goto out;
  6835. }
  6836. }
  6837. /*
  6838. * Minimal mode - state operations are allowed to use machine
  6839. * credential. Note this already happens by default, so the
  6840. * client doesn't have to do anything more than the negotiation.
  6841. *
  6842. * NOTE: we don't care if EXCHANGE_ID is in the list -
  6843. * we're already using the machine cred for exchange_id
  6844. * and will never use a different cred.
  6845. */
  6846. if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
  6847. test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
  6848. test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
  6849. test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
  6850. dfprintk(MOUNT, "sp4_mach_cred:\n");
  6851. dfprintk(MOUNT, " minimal mode enabled\n");
  6852. __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
  6853. } else {
  6854. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  6855. ret = -EINVAL;
  6856. goto out;
  6857. }
  6858. if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
  6859. test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
  6860. test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
  6861. test_bit(OP_LOCKU, sp->allow.u.longs)) {
  6862. dfprintk(MOUNT, " cleanup mode enabled\n");
  6863. __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
  6864. }
  6865. if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
  6866. dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
  6867. __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
  6868. }
  6869. if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
  6870. test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
  6871. dfprintk(MOUNT, " secinfo mode enabled\n");
  6872. __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
  6873. }
  6874. if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
  6875. test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
  6876. dfprintk(MOUNT, " stateid mode enabled\n");
  6877. __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
  6878. }
  6879. if (test_bit(OP_WRITE, sp->allow.u.longs)) {
  6880. dfprintk(MOUNT, " write mode enabled\n");
  6881. __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
  6882. }
  6883. if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
  6884. dfprintk(MOUNT, " commit mode enabled\n");
  6885. __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
  6886. }
  6887. }
  6888. out:
  6889. clp->cl_sp4_flags = flags;
  6890. return 0;
  6891. }
  6892. struct nfs41_exchange_id_data {
  6893. struct nfs41_exchange_id_res res;
  6894. struct nfs41_exchange_id_args args;
  6895. };
  6896. static void nfs4_exchange_id_release(void *data)
  6897. {
  6898. struct nfs41_exchange_id_data *cdata =
  6899. (struct nfs41_exchange_id_data *)data;
  6900. nfs_put_client(cdata->args.client);
  6901. kfree(cdata->res.impl_id);
  6902. kfree(cdata->res.server_scope);
  6903. kfree(cdata->res.server_owner);
  6904. kfree(cdata);
  6905. }
  6906. static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
  6907. .rpc_release = nfs4_exchange_id_release,
  6908. };
  6909. /*
  6910. * _nfs4_proc_exchange_id()
  6911. *
  6912. * Wrapper for EXCHANGE_ID operation.
  6913. */
  6914. static struct rpc_task *
  6915. nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
  6916. u32 sp4_how, struct rpc_xprt *xprt)
  6917. {
  6918. struct rpc_message msg = {
  6919. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  6920. .rpc_cred = cred,
  6921. };
  6922. struct rpc_task_setup task_setup_data = {
  6923. .rpc_client = clp->cl_rpcclient,
  6924. .callback_ops = &nfs4_exchange_id_call_ops,
  6925. .rpc_message = &msg,
  6926. .flags = RPC_TASK_TIMEOUT,
  6927. };
  6928. struct nfs41_exchange_id_data *calldata;
  6929. int status;
  6930. if (!refcount_inc_not_zero(&clp->cl_count))
  6931. return ERR_PTR(-EIO);
  6932. status = -ENOMEM;
  6933. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  6934. if (!calldata)
  6935. goto out;
  6936. nfs4_init_boot_verifier(clp, &calldata->args.verifier);
  6937. status = nfs4_init_uniform_client_string(clp);
  6938. if (status)
  6939. goto out_calldata;
  6940. calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
  6941. GFP_NOFS);
  6942. status = -ENOMEM;
  6943. if (unlikely(calldata->res.server_owner == NULL))
  6944. goto out_calldata;
  6945. calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
  6946. GFP_NOFS);
  6947. if (unlikely(calldata->res.server_scope == NULL))
  6948. goto out_server_owner;
  6949. calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
  6950. if (unlikely(calldata->res.impl_id == NULL))
  6951. goto out_server_scope;
  6952. switch (sp4_how) {
  6953. case SP4_NONE:
  6954. calldata->args.state_protect.how = SP4_NONE;
  6955. break;
  6956. case SP4_MACH_CRED:
  6957. calldata->args.state_protect = nfs4_sp4_mach_cred_request;
  6958. break;
  6959. default:
  6960. /* unsupported! */
  6961. WARN_ON_ONCE(1);
  6962. status = -EINVAL;
  6963. goto out_impl_id;
  6964. }
  6965. if (xprt) {
  6966. task_setup_data.rpc_xprt = xprt;
  6967. task_setup_data.flags |= RPC_TASK_SOFTCONN;
  6968. memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
  6969. sizeof(calldata->args.verifier.data));
  6970. }
  6971. calldata->args.client = clp;
  6972. calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
  6973. EXCHGID4_FLAG_BIND_PRINC_STATEID;
  6974. #ifdef CONFIG_NFS_V4_1_MIGRATION
  6975. calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
  6976. #endif
  6977. msg.rpc_argp = &calldata->args;
  6978. msg.rpc_resp = &calldata->res;
  6979. task_setup_data.callback_data = calldata;
  6980. return rpc_run_task(&task_setup_data);
  6981. out_impl_id:
  6982. kfree(calldata->res.impl_id);
  6983. out_server_scope:
  6984. kfree(calldata->res.server_scope);
  6985. out_server_owner:
  6986. kfree(calldata->res.server_owner);
  6987. out_calldata:
  6988. kfree(calldata);
  6989. out:
  6990. nfs_put_client(clp);
  6991. return ERR_PTR(status);
  6992. }
  6993. /*
  6994. * _nfs4_proc_exchange_id()
  6995. *
  6996. * Wrapper for EXCHANGE_ID operation.
  6997. */
  6998. static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
  6999. u32 sp4_how)
  7000. {
  7001. struct rpc_task *task;
  7002. struct nfs41_exchange_id_args *argp;
  7003. struct nfs41_exchange_id_res *resp;
  7004. int status;
  7005. task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
  7006. if (IS_ERR(task))
  7007. return PTR_ERR(task);
  7008. argp = task->tk_msg.rpc_argp;
  7009. resp = task->tk_msg.rpc_resp;
  7010. status = task->tk_status;
  7011. if (status != 0)
  7012. goto out;
  7013. status = nfs4_check_cl_exchange_flags(resp->flags);
  7014. if (status != 0)
  7015. goto out;
  7016. status = nfs4_sp4_select_mode(clp, &resp->state_protect);
  7017. if (status != 0)
  7018. goto out;
  7019. clp->cl_clientid = resp->clientid;
  7020. clp->cl_exchange_flags = resp->flags;
  7021. clp->cl_seqid = resp->seqid;
  7022. /* Client ID is not confirmed */
  7023. if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
  7024. clear_bit(NFS4_SESSION_ESTABLISHED,
  7025. &clp->cl_session->session_state);
  7026. if (clp->cl_serverscope != NULL &&
  7027. !nfs41_same_server_scope(clp->cl_serverscope,
  7028. resp->server_scope)) {
  7029. dprintk("%s: server_scope mismatch detected\n",
  7030. __func__);
  7031. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  7032. }
  7033. swap(clp->cl_serverowner, resp->server_owner);
  7034. swap(clp->cl_serverscope, resp->server_scope);
  7035. swap(clp->cl_implid, resp->impl_id);
  7036. /* Save the EXCHANGE_ID verifier session trunk tests */
  7037. memcpy(clp->cl_confirm.data, argp->verifier.data,
  7038. sizeof(clp->cl_confirm.data));
  7039. out:
  7040. trace_nfs4_exchange_id(clp, status);
  7041. rpc_put_task(task);
  7042. return status;
  7043. }
  7044. /*
  7045. * nfs4_proc_exchange_id()
  7046. *
  7047. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  7048. *
  7049. * Since the clientid has expired, all compounds using sessions
  7050. * associated with the stale clientid will be returning
  7051. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  7052. * be in some phase of session reset.
  7053. *
  7054. * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
  7055. */
  7056. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  7057. {
  7058. rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
  7059. int status;
  7060. /* try SP4_MACH_CRED if krb5i/p */
  7061. if (authflavor == RPC_AUTH_GSS_KRB5I ||
  7062. authflavor == RPC_AUTH_GSS_KRB5P) {
  7063. status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
  7064. if (!status)
  7065. return 0;
  7066. }
  7067. /* try SP4_NONE */
  7068. return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
  7069. }
  7070. /**
  7071. * nfs4_test_session_trunk
  7072. *
  7073. * This is an add_xprt_test() test function called from
  7074. * rpc_clnt_setup_test_and_add_xprt.
  7075. *
  7076. * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
  7077. * and is dereferrenced in nfs4_exchange_id_release
  7078. *
  7079. * Upon success, add the new transport to the rpc_clnt
  7080. *
  7081. * @clnt: struct rpc_clnt to get new transport
  7082. * @xprt: the rpc_xprt to test
  7083. * @data: call data for _nfs4_proc_exchange_id.
  7084. */
  7085. int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
  7086. void *data)
  7087. {
  7088. struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
  7089. struct rpc_task *task;
  7090. int status;
  7091. u32 sp4_how;
  7092. dprintk("--> %s try %s\n", __func__,
  7093. xprt->address_strings[RPC_DISPLAY_ADDR]);
  7094. sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
  7095. /* Test connection for session trunking. Async exchange_id call */
  7096. task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
  7097. if (IS_ERR(task))
  7098. return PTR_ERR(task);
  7099. status = task->tk_status;
  7100. if (status == 0)
  7101. status = nfs4_detect_session_trunking(adata->clp,
  7102. task->tk_msg.rpc_resp, xprt);
  7103. rpc_put_task(task);
  7104. return status;
  7105. }
  7106. EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
  7107. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  7108. struct rpc_cred *cred)
  7109. {
  7110. struct rpc_message msg = {
  7111. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  7112. .rpc_argp = clp,
  7113. .rpc_cred = cred,
  7114. };
  7115. int status;
  7116. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7117. trace_nfs4_destroy_clientid(clp, status);
  7118. if (status)
  7119. dprintk("NFS: Got error %d from the server %s on "
  7120. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  7121. return status;
  7122. }
  7123. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  7124. struct rpc_cred *cred)
  7125. {
  7126. unsigned int loop;
  7127. int ret;
  7128. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  7129. ret = _nfs4_proc_destroy_clientid(clp, cred);
  7130. switch (ret) {
  7131. case -NFS4ERR_DELAY:
  7132. case -NFS4ERR_CLIENTID_BUSY:
  7133. ssleep(1);
  7134. break;
  7135. default:
  7136. return ret;
  7137. }
  7138. }
  7139. return 0;
  7140. }
  7141. int nfs4_destroy_clientid(struct nfs_client *clp)
  7142. {
  7143. struct rpc_cred *cred;
  7144. int ret = 0;
  7145. if (clp->cl_mvops->minor_version < 1)
  7146. goto out;
  7147. if (clp->cl_exchange_flags == 0)
  7148. goto out;
  7149. if (clp->cl_preserve_clid)
  7150. goto out;
  7151. cred = nfs4_get_clid_cred(clp);
  7152. ret = nfs4_proc_destroy_clientid(clp, cred);
  7153. if (cred)
  7154. put_rpccred(cred);
  7155. switch (ret) {
  7156. case 0:
  7157. case -NFS4ERR_STALE_CLIENTID:
  7158. clp->cl_exchange_flags = 0;
  7159. }
  7160. out:
  7161. return ret;
  7162. }
  7163. struct nfs4_get_lease_time_data {
  7164. struct nfs4_get_lease_time_args *args;
  7165. struct nfs4_get_lease_time_res *res;
  7166. struct nfs_client *clp;
  7167. };
  7168. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  7169. void *calldata)
  7170. {
  7171. struct nfs4_get_lease_time_data *data =
  7172. (struct nfs4_get_lease_time_data *)calldata;
  7173. dprintk("--> %s\n", __func__);
  7174. /* just setup sequence, do not trigger session recovery
  7175. since we're invoked within one */
  7176. nfs4_setup_sequence(data->clp,
  7177. &data->args->la_seq_args,
  7178. &data->res->lr_seq_res,
  7179. task);
  7180. dprintk("<-- %s\n", __func__);
  7181. }
  7182. /*
  7183. * Called from nfs4_state_manager thread for session setup, so don't recover
  7184. * from sequence operation or clientid errors.
  7185. */
  7186. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  7187. {
  7188. struct nfs4_get_lease_time_data *data =
  7189. (struct nfs4_get_lease_time_data *)calldata;
  7190. dprintk("--> %s\n", __func__);
  7191. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  7192. return;
  7193. switch (task->tk_status) {
  7194. case -NFS4ERR_DELAY:
  7195. case -NFS4ERR_GRACE:
  7196. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  7197. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  7198. task->tk_status = 0;
  7199. /* fall through */
  7200. case -NFS4ERR_RETRY_UNCACHED_REP:
  7201. rpc_restart_call_prepare(task);
  7202. return;
  7203. }
  7204. dprintk("<-- %s\n", __func__);
  7205. }
  7206. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  7207. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  7208. .rpc_call_done = nfs4_get_lease_time_done,
  7209. };
  7210. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  7211. {
  7212. struct rpc_task *task;
  7213. struct nfs4_get_lease_time_args args;
  7214. struct nfs4_get_lease_time_res res = {
  7215. .lr_fsinfo = fsinfo,
  7216. };
  7217. struct nfs4_get_lease_time_data data = {
  7218. .args = &args,
  7219. .res = &res,
  7220. .clp = clp,
  7221. };
  7222. struct rpc_message msg = {
  7223. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  7224. .rpc_argp = &args,
  7225. .rpc_resp = &res,
  7226. };
  7227. struct rpc_task_setup task_setup = {
  7228. .rpc_client = clp->cl_rpcclient,
  7229. .rpc_message = &msg,
  7230. .callback_ops = &nfs4_get_lease_time_ops,
  7231. .callback_data = &data,
  7232. .flags = RPC_TASK_TIMEOUT,
  7233. };
  7234. int status;
  7235. nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
  7236. task = rpc_run_task(&task_setup);
  7237. if (IS_ERR(task))
  7238. return PTR_ERR(task);
  7239. status = task->tk_status;
  7240. rpc_put_task(task);
  7241. return status;
  7242. }
  7243. /*
  7244. * Initialize the values to be used by the client in CREATE_SESSION
  7245. * If nfs4_init_session set the fore channel request and response sizes,
  7246. * use them.
  7247. *
  7248. * Set the back channel max_resp_sz_cached to zero to force the client to
  7249. * always set csa_cachethis to FALSE because the current implementation
  7250. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  7251. */
  7252. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
  7253. struct rpc_clnt *clnt)
  7254. {
  7255. unsigned int max_rqst_sz, max_resp_sz;
  7256. unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
  7257. max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
  7258. max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
  7259. /* Fore channel attributes */
  7260. args->fc_attrs.max_rqst_sz = max_rqst_sz;
  7261. args->fc_attrs.max_resp_sz = max_resp_sz;
  7262. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  7263. args->fc_attrs.max_reqs = max_session_slots;
  7264. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  7265. "max_ops=%u max_reqs=%u\n",
  7266. __func__,
  7267. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  7268. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  7269. /* Back channel attributes */
  7270. args->bc_attrs.max_rqst_sz = max_bc_payload;
  7271. args->bc_attrs.max_resp_sz = max_bc_payload;
  7272. args->bc_attrs.max_resp_sz_cached = 0;
  7273. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  7274. args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
  7275. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  7276. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  7277. __func__,
  7278. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  7279. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  7280. args->bc_attrs.max_reqs);
  7281. }
  7282. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
  7283. struct nfs41_create_session_res *res)
  7284. {
  7285. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  7286. struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
  7287. if (rcvd->max_resp_sz > sent->max_resp_sz)
  7288. return -EINVAL;
  7289. /*
  7290. * Our requested max_ops is the minimum we need; we're not
  7291. * prepared to break up compounds into smaller pieces than that.
  7292. * So, no point even trying to continue if the server won't
  7293. * cooperate:
  7294. */
  7295. if (rcvd->max_ops < sent->max_ops)
  7296. return -EINVAL;
  7297. if (rcvd->max_reqs == 0)
  7298. return -EINVAL;
  7299. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  7300. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  7301. return 0;
  7302. }
  7303. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
  7304. struct nfs41_create_session_res *res)
  7305. {
  7306. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  7307. struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
  7308. if (!(res->flags & SESSION4_BACK_CHAN))
  7309. goto out;
  7310. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  7311. return -EINVAL;
  7312. if (rcvd->max_resp_sz < sent->max_resp_sz)
  7313. return -EINVAL;
  7314. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  7315. return -EINVAL;
  7316. if (rcvd->max_ops > sent->max_ops)
  7317. return -EINVAL;
  7318. if (rcvd->max_reqs > sent->max_reqs)
  7319. return -EINVAL;
  7320. out:
  7321. return 0;
  7322. }
  7323. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  7324. struct nfs41_create_session_res *res)
  7325. {
  7326. int ret;
  7327. ret = nfs4_verify_fore_channel_attrs(args, res);
  7328. if (ret)
  7329. return ret;
  7330. return nfs4_verify_back_channel_attrs(args, res);
  7331. }
  7332. static void nfs4_update_session(struct nfs4_session *session,
  7333. struct nfs41_create_session_res *res)
  7334. {
  7335. nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
  7336. /* Mark client id and session as being confirmed */
  7337. session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  7338. set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
  7339. session->flags = res->flags;
  7340. memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
  7341. if (res->flags & SESSION4_BACK_CHAN)
  7342. memcpy(&session->bc_attrs, &res->bc_attrs,
  7343. sizeof(session->bc_attrs));
  7344. }
  7345. static int _nfs4_proc_create_session(struct nfs_client *clp,
  7346. struct rpc_cred *cred)
  7347. {
  7348. struct nfs4_session *session = clp->cl_session;
  7349. struct nfs41_create_session_args args = {
  7350. .client = clp,
  7351. .clientid = clp->cl_clientid,
  7352. .seqid = clp->cl_seqid,
  7353. .cb_program = NFS4_CALLBACK,
  7354. };
  7355. struct nfs41_create_session_res res;
  7356. struct rpc_message msg = {
  7357. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  7358. .rpc_argp = &args,
  7359. .rpc_resp = &res,
  7360. .rpc_cred = cred,
  7361. };
  7362. int status;
  7363. nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
  7364. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  7365. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7366. trace_nfs4_create_session(clp, status);
  7367. switch (status) {
  7368. case -NFS4ERR_STALE_CLIENTID:
  7369. case -NFS4ERR_DELAY:
  7370. case -ETIMEDOUT:
  7371. case -EACCES:
  7372. case -EAGAIN:
  7373. goto out;
  7374. };
  7375. clp->cl_seqid++;
  7376. if (!status) {
  7377. /* Verify the session's negotiated channel_attrs values */
  7378. status = nfs4_verify_channel_attrs(&args, &res);
  7379. /* Increment the clientid slot sequence id */
  7380. if (status)
  7381. goto out;
  7382. nfs4_update_session(session, &res);
  7383. }
  7384. out:
  7385. return status;
  7386. }
  7387. /*
  7388. * Issues a CREATE_SESSION operation to the server.
  7389. * It is the responsibility of the caller to verify the session is
  7390. * expired before calling this routine.
  7391. */
  7392. int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
  7393. {
  7394. int status;
  7395. unsigned *ptr;
  7396. struct nfs4_session *session = clp->cl_session;
  7397. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  7398. status = _nfs4_proc_create_session(clp, cred);
  7399. if (status)
  7400. goto out;
  7401. /* Init or reset the session slot tables */
  7402. status = nfs4_setup_session_slot_tables(session);
  7403. dprintk("slot table setup returned %d\n", status);
  7404. if (status)
  7405. goto out;
  7406. ptr = (unsigned *)&session->sess_id.data[0];
  7407. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  7408. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  7409. out:
  7410. dprintk("<-- %s\n", __func__);
  7411. return status;
  7412. }
  7413. /*
  7414. * Issue the over-the-wire RPC DESTROY_SESSION.
  7415. * The caller must serialize access to this routine.
  7416. */
  7417. int nfs4_proc_destroy_session(struct nfs4_session *session,
  7418. struct rpc_cred *cred)
  7419. {
  7420. struct rpc_message msg = {
  7421. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  7422. .rpc_argp = session,
  7423. .rpc_cred = cred,
  7424. };
  7425. int status = 0;
  7426. dprintk("--> nfs4_proc_destroy_session\n");
  7427. /* session is still being setup */
  7428. if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
  7429. return 0;
  7430. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7431. trace_nfs4_destroy_session(session->clp, status);
  7432. if (status)
  7433. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  7434. "Session has been destroyed regardless...\n", status);
  7435. dprintk("<-- nfs4_proc_destroy_session\n");
  7436. return status;
  7437. }
  7438. /*
  7439. * Renew the cl_session lease.
  7440. */
  7441. struct nfs4_sequence_data {
  7442. struct nfs_client *clp;
  7443. struct nfs4_sequence_args args;
  7444. struct nfs4_sequence_res res;
  7445. };
  7446. static void nfs41_sequence_release(void *data)
  7447. {
  7448. struct nfs4_sequence_data *calldata = data;
  7449. struct nfs_client *clp = calldata->clp;
  7450. if (refcount_read(&clp->cl_count) > 1)
  7451. nfs4_schedule_state_renewal(clp);
  7452. nfs_put_client(clp);
  7453. kfree(calldata);
  7454. }
  7455. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  7456. {
  7457. switch(task->tk_status) {
  7458. case -NFS4ERR_DELAY:
  7459. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  7460. return -EAGAIN;
  7461. default:
  7462. nfs4_schedule_lease_recovery(clp);
  7463. }
  7464. return 0;
  7465. }
  7466. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  7467. {
  7468. struct nfs4_sequence_data *calldata = data;
  7469. struct nfs_client *clp = calldata->clp;
  7470. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  7471. return;
  7472. trace_nfs4_sequence(clp, task->tk_status);
  7473. if (task->tk_status < 0) {
  7474. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  7475. if (refcount_read(&clp->cl_count) == 1)
  7476. goto out;
  7477. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  7478. rpc_restart_call_prepare(task);
  7479. return;
  7480. }
  7481. }
  7482. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  7483. out:
  7484. dprintk("<-- %s\n", __func__);
  7485. }
  7486. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  7487. {
  7488. struct nfs4_sequence_data *calldata = data;
  7489. struct nfs_client *clp = calldata->clp;
  7490. struct nfs4_sequence_args *args;
  7491. struct nfs4_sequence_res *res;
  7492. args = task->tk_msg.rpc_argp;
  7493. res = task->tk_msg.rpc_resp;
  7494. nfs4_setup_sequence(clp, args, res, task);
  7495. }
  7496. static const struct rpc_call_ops nfs41_sequence_ops = {
  7497. .rpc_call_done = nfs41_sequence_call_done,
  7498. .rpc_call_prepare = nfs41_sequence_prepare,
  7499. .rpc_release = nfs41_sequence_release,
  7500. };
  7501. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  7502. struct rpc_cred *cred,
  7503. struct nfs4_slot *slot,
  7504. bool is_privileged)
  7505. {
  7506. struct nfs4_sequence_data *calldata;
  7507. struct rpc_message msg = {
  7508. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  7509. .rpc_cred = cred,
  7510. };
  7511. struct rpc_task_setup task_setup_data = {
  7512. .rpc_client = clp->cl_rpcclient,
  7513. .rpc_message = &msg,
  7514. .callback_ops = &nfs41_sequence_ops,
  7515. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
  7516. };
  7517. struct rpc_task *ret;
  7518. ret = ERR_PTR(-EIO);
  7519. if (!refcount_inc_not_zero(&clp->cl_count))
  7520. goto out_err;
  7521. ret = ERR_PTR(-ENOMEM);
  7522. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  7523. if (calldata == NULL)
  7524. goto out_put_clp;
  7525. nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
  7526. nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
  7527. msg.rpc_argp = &calldata->args;
  7528. msg.rpc_resp = &calldata->res;
  7529. calldata->clp = clp;
  7530. task_setup_data.callback_data = calldata;
  7531. ret = rpc_run_task(&task_setup_data);
  7532. if (IS_ERR(ret))
  7533. goto out_err;
  7534. return ret;
  7535. out_put_clp:
  7536. nfs_put_client(clp);
  7537. out_err:
  7538. nfs41_release_slot(slot);
  7539. return ret;
  7540. }
  7541. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  7542. {
  7543. struct rpc_task *task;
  7544. int ret = 0;
  7545. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  7546. return -EAGAIN;
  7547. task = _nfs41_proc_sequence(clp, cred, NULL, false);
  7548. if (IS_ERR(task))
  7549. ret = PTR_ERR(task);
  7550. else
  7551. rpc_put_task_async(task);
  7552. dprintk("<-- %s status=%d\n", __func__, ret);
  7553. return ret;
  7554. }
  7555. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  7556. {
  7557. struct rpc_task *task;
  7558. int ret;
  7559. task = _nfs41_proc_sequence(clp, cred, NULL, true);
  7560. if (IS_ERR(task)) {
  7561. ret = PTR_ERR(task);
  7562. goto out;
  7563. }
  7564. ret = rpc_wait_for_completion_task(task);
  7565. if (!ret)
  7566. ret = task->tk_status;
  7567. rpc_put_task(task);
  7568. out:
  7569. dprintk("<-- %s status=%d\n", __func__, ret);
  7570. return ret;
  7571. }
  7572. struct nfs4_reclaim_complete_data {
  7573. struct nfs_client *clp;
  7574. struct nfs41_reclaim_complete_args arg;
  7575. struct nfs41_reclaim_complete_res res;
  7576. };
  7577. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  7578. {
  7579. struct nfs4_reclaim_complete_data *calldata = data;
  7580. nfs4_setup_sequence(calldata->clp,
  7581. &calldata->arg.seq_args,
  7582. &calldata->res.seq_res,
  7583. task);
  7584. }
  7585. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  7586. {
  7587. switch(task->tk_status) {
  7588. case 0:
  7589. wake_up_all(&clp->cl_lock_waitq);
  7590. /* Fallthrough */
  7591. case -NFS4ERR_COMPLETE_ALREADY:
  7592. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  7593. break;
  7594. case -NFS4ERR_DELAY:
  7595. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  7596. /* fall through */
  7597. case -NFS4ERR_RETRY_UNCACHED_REP:
  7598. return -EAGAIN;
  7599. case -NFS4ERR_BADSESSION:
  7600. case -NFS4ERR_DEADSESSION:
  7601. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  7602. nfs4_schedule_session_recovery(clp->cl_session,
  7603. task->tk_status);
  7604. break;
  7605. default:
  7606. nfs4_schedule_lease_recovery(clp);
  7607. }
  7608. return 0;
  7609. }
  7610. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  7611. {
  7612. struct nfs4_reclaim_complete_data *calldata = data;
  7613. struct nfs_client *clp = calldata->clp;
  7614. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  7615. dprintk("--> %s\n", __func__);
  7616. if (!nfs41_sequence_done(task, res))
  7617. return;
  7618. trace_nfs4_reclaim_complete(clp, task->tk_status);
  7619. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  7620. rpc_restart_call_prepare(task);
  7621. return;
  7622. }
  7623. dprintk("<-- %s\n", __func__);
  7624. }
  7625. static void nfs4_free_reclaim_complete_data(void *data)
  7626. {
  7627. struct nfs4_reclaim_complete_data *calldata = data;
  7628. kfree(calldata);
  7629. }
  7630. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  7631. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  7632. .rpc_call_done = nfs4_reclaim_complete_done,
  7633. .rpc_release = nfs4_free_reclaim_complete_data,
  7634. };
  7635. /*
  7636. * Issue a global reclaim complete.
  7637. */
  7638. static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
  7639. struct rpc_cred *cred)
  7640. {
  7641. struct nfs4_reclaim_complete_data *calldata;
  7642. struct rpc_task *task;
  7643. struct rpc_message msg = {
  7644. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  7645. .rpc_cred = cred,
  7646. };
  7647. struct rpc_task_setup task_setup_data = {
  7648. .rpc_client = clp->cl_rpcclient,
  7649. .rpc_message = &msg,
  7650. .callback_ops = &nfs4_reclaim_complete_call_ops,
  7651. .flags = RPC_TASK_ASYNC,
  7652. };
  7653. int status = -ENOMEM;
  7654. dprintk("--> %s\n", __func__);
  7655. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  7656. if (calldata == NULL)
  7657. goto out;
  7658. calldata->clp = clp;
  7659. calldata->arg.one_fs = 0;
  7660. nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
  7661. msg.rpc_argp = &calldata->arg;
  7662. msg.rpc_resp = &calldata->res;
  7663. task_setup_data.callback_data = calldata;
  7664. task = rpc_run_task(&task_setup_data);
  7665. if (IS_ERR(task)) {
  7666. status = PTR_ERR(task);
  7667. goto out;
  7668. }
  7669. status = rpc_wait_for_completion_task(task);
  7670. if (status == 0)
  7671. status = task->tk_status;
  7672. rpc_put_task(task);
  7673. out:
  7674. dprintk("<-- %s status=%d\n", __func__, status);
  7675. return status;
  7676. }
  7677. static void
  7678. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  7679. {
  7680. struct nfs4_layoutget *lgp = calldata;
  7681. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  7682. dprintk("--> %s\n", __func__);
  7683. nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
  7684. &lgp->res.seq_res, task);
  7685. dprintk("<-- %s\n", __func__);
  7686. }
  7687. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  7688. {
  7689. struct nfs4_layoutget *lgp = calldata;
  7690. dprintk("--> %s\n", __func__);
  7691. nfs41_sequence_process(task, &lgp->res.seq_res);
  7692. dprintk("<-- %s\n", __func__);
  7693. }
  7694. static int
  7695. nfs4_layoutget_handle_exception(struct rpc_task *task,
  7696. struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
  7697. {
  7698. struct inode *inode = lgp->args.inode;
  7699. struct nfs_server *server = NFS_SERVER(inode);
  7700. struct pnfs_layout_hdr *lo;
  7701. int nfs4err = task->tk_status;
  7702. int err, status = 0;
  7703. LIST_HEAD(head);
  7704. dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
  7705. switch (nfs4err) {
  7706. case 0:
  7707. goto out;
  7708. /*
  7709. * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
  7710. * on the file. set tk_status to -ENODATA to tell upper layer to
  7711. * retry go inband.
  7712. */
  7713. case -NFS4ERR_LAYOUTUNAVAILABLE:
  7714. status = -ENODATA;
  7715. goto out;
  7716. /*
  7717. * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
  7718. * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
  7719. */
  7720. case -NFS4ERR_BADLAYOUT:
  7721. status = -EOVERFLOW;
  7722. goto out;
  7723. /*
  7724. * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
  7725. * (or clients) writing to the same RAID stripe except when
  7726. * the minlength argument is 0 (see RFC5661 section 18.43.3).
  7727. *
  7728. * Treat it like we would RECALLCONFLICT -- we retry for a little
  7729. * while, and then eventually give up.
  7730. */
  7731. case -NFS4ERR_LAYOUTTRYLATER:
  7732. if (lgp->args.minlength == 0) {
  7733. status = -EOVERFLOW;
  7734. goto out;
  7735. }
  7736. status = -EBUSY;
  7737. break;
  7738. case -NFS4ERR_RECALLCONFLICT:
  7739. status = -ERECALLCONFLICT;
  7740. break;
  7741. case -NFS4ERR_DELEG_REVOKED:
  7742. case -NFS4ERR_ADMIN_REVOKED:
  7743. case -NFS4ERR_EXPIRED:
  7744. case -NFS4ERR_BAD_STATEID:
  7745. exception->timeout = 0;
  7746. spin_lock(&inode->i_lock);
  7747. lo = NFS_I(inode)->layout;
  7748. /* If the open stateid was bad, then recover it. */
  7749. if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
  7750. !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
  7751. spin_unlock(&inode->i_lock);
  7752. exception->state = lgp->args.ctx->state;
  7753. exception->stateid = &lgp->args.stateid;
  7754. break;
  7755. }
  7756. /*
  7757. * Mark the bad layout state as invalid, then retry
  7758. */
  7759. pnfs_mark_layout_stateid_invalid(lo, &head);
  7760. spin_unlock(&inode->i_lock);
  7761. nfs_commit_inode(inode, 0);
  7762. pnfs_free_lseg_list(&head);
  7763. status = -EAGAIN;
  7764. goto out;
  7765. }
  7766. nfs4_sequence_free_slot(&lgp->res.seq_res);
  7767. err = nfs4_handle_exception(server, nfs4err, exception);
  7768. if (!status) {
  7769. if (exception->retry)
  7770. status = -EAGAIN;
  7771. else
  7772. status = err;
  7773. }
  7774. out:
  7775. dprintk("<-- %s\n", __func__);
  7776. return status;
  7777. }
  7778. size_t max_response_pages(struct nfs_server *server)
  7779. {
  7780. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  7781. return nfs_page_array_len(0, max_resp_sz);
  7782. }
  7783. static void nfs4_layoutget_release(void *calldata)
  7784. {
  7785. struct nfs4_layoutget *lgp = calldata;
  7786. dprintk("--> %s\n", __func__);
  7787. nfs4_sequence_free_slot(&lgp->res.seq_res);
  7788. pnfs_layoutget_free(lgp);
  7789. dprintk("<-- %s\n", __func__);
  7790. }
  7791. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  7792. .rpc_call_prepare = nfs4_layoutget_prepare,
  7793. .rpc_call_done = nfs4_layoutget_done,
  7794. .rpc_release = nfs4_layoutget_release,
  7795. };
  7796. struct pnfs_layout_segment *
  7797. nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
  7798. {
  7799. struct inode *inode = lgp->args.inode;
  7800. struct nfs_server *server = NFS_SERVER(inode);
  7801. struct rpc_task *task;
  7802. struct rpc_message msg = {
  7803. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  7804. .rpc_argp = &lgp->args,
  7805. .rpc_resp = &lgp->res,
  7806. .rpc_cred = lgp->cred,
  7807. };
  7808. struct rpc_task_setup task_setup_data = {
  7809. .rpc_client = server->client,
  7810. .rpc_message = &msg,
  7811. .callback_ops = &nfs4_layoutget_call_ops,
  7812. .callback_data = lgp,
  7813. .flags = RPC_TASK_ASYNC,
  7814. };
  7815. struct pnfs_layout_segment *lseg = NULL;
  7816. struct nfs4_exception exception = {
  7817. .inode = inode,
  7818. .timeout = *timeout,
  7819. };
  7820. int status = 0;
  7821. dprintk("--> %s\n", __func__);
  7822. /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
  7823. pnfs_get_layout_hdr(NFS_I(inode)->layout);
  7824. nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
  7825. task = rpc_run_task(&task_setup_data);
  7826. if (IS_ERR(task))
  7827. return ERR_CAST(task);
  7828. status = rpc_wait_for_completion_task(task);
  7829. if (status == 0) {
  7830. status = nfs4_layoutget_handle_exception(task, lgp, &exception);
  7831. *timeout = exception.timeout;
  7832. }
  7833. trace_nfs4_layoutget(lgp->args.ctx,
  7834. &lgp->args.range,
  7835. &lgp->res.range,
  7836. &lgp->res.stateid,
  7837. status);
  7838. /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
  7839. if (status == 0 && lgp->res.layoutp->len)
  7840. lseg = pnfs_layout_process(lgp);
  7841. rpc_put_task(task);
  7842. dprintk("<-- %s status=%d\n", __func__, status);
  7843. if (status)
  7844. return ERR_PTR(status);
  7845. return lseg;
  7846. }
  7847. static void
  7848. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  7849. {
  7850. struct nfs4_layoutreturn *lrp = calldata;
  7851. dprintk("--> %s\n", __func__);
  7852. nfs4_setup_sequence(lrp->clp,
  7853. &lrp->args.seq_args,
  7854. &lrp->res.seq_res,
  7855. task);
  7856. }
  7857. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  7858. {
  7859. struct nfs4_layoutreturn *lrp = calldata;
  7860. struct nfs_server *server;
  7861. dprintk("--> %s\n", __func__);
  7862. if (!nfs41_sequence_process(task, &lrp->res.seq_res))
  7863. return;
  7864. server = NFS_SERVER(lrp->args.inode);
  7865. switch (task->tk_status) {
  7866. case -NFS4ERR_OLD_STATEID:
  7867. if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
  7868. lrp->args.inode))
  7869. goto out_restart;
  7870. /* Fallthrough */
  7871. default:
  7872. task->tk_status = 0;
  7873. /* Fallthrough */
  7874. case 0:
  7875. break;
  7876. case -NFS4ERR_DELAY:
  7877. if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
  7878. break;
  7879. goto out_restart;
  7880. }
  7881. dprintk("<-- %s\n", __func__);
  7882. return;
  7883. out_restart:
  7884. task->tk_status = 0;
  7885. nfs4_sequence_free_slot(&lrp->res.seq_res);
  7886. rpc_restart_call_prepare(task);
  7887. }
  7888. static void nfs4_layoutreturn_release(void *calldata)
  7889. {
  7890. struct nfs4_layoutreturn *lrp = calldata;
  7891. struct pnfs_layout_hdr *lo = lrp->args.layout;
  7892. dprintk("--> %s\n", __func__);
  7893. pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
  7894. lrp->res.lrs_present ? &lrp->res.stateid : NULL);
  7895. nfs4_sequence_free_slot(&lrp->res.seq_res);
  7896. if (lrp->ld_private.ops && lrp->ld_private.ops->free)
  7897. lrp->ld_private.ops->free(&lrp->ld_private);
  7898. pnfs_put_layout_hdr(lrp->args.layout);
  7899. nfs_iput_and_deactive(lrp->inode);
  7900. kfree(calldata);
  7901. dprintk("<-- %s\n", __func__);
  7902. }
  7903. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  7904. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  7905. .rpc_call_done = nfs4_layoutreturn_done,
  7906. .rpc_release = nfs4_layoutreturn_release,
  7907. };
  7908. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
  7909. {
  7910. struct rpc_task *task;
  7911. struct rpc_message msg = {
  7912. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  7913. .rpc_argp = &lrp->args,
  7914. .rpc_resp = &lrp->res,
  7915. .rpc_cred = lrp->cred,
  7916. };
  7917. struct rpc_task_setup task_setup_data = {
  7918. .rpc_client = NFS_SERVER(lrp->args.inode)->client,
  7919. .rpc_message = &msg,
  7920. .callback_ops = &nfs4_layoutreturn_call_ops,
  7921. .callback_data = lrp,
  7922. };
  7923. int status = 0;
  7924. nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
  7925. NFS_SP4_MACH_CRED_PNFS_CLEANUP,
  7926. &task_setup_data.rpc_client, &msg);
  7927. dprintk("--> %s\n", __func__);
  7928. if (!sync) {
  7929. lrp->inode = nfs_igrab_and_active(lrp->args.inode);
  7930. if (!lrp->inode) {
  7931. nfs4_layoutreturn_release(lrp);
  7932. return -EAGAIN;
  7933. }
  7934. task_setup_data.flags |= RPC_TASK_ASYNC;
  7935. }
  7936. nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
  7937. task = rpc_run_task(&task_setup_data);
  7938. if (IS_ERR(task))
  7939. return PTR_ERR(task);
  7940. if (sync)
  7941. status = task->tk_status;
  7942. trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
  7943. dprintk("<-- %s status=%d\n", __func__, status);
  7944. rpc_put_task(task);
  7945. return status;
  7946. }
  7947. static int
  7948. _nfs4_proc_getdeviceinfo(struct nfs_server *server,
  7949. struct pnfs_device *pdev,
  7950. struct rpc_cred *cred)
  7951. {
  7952. struct nfs4_getdeviceinfo_args args = {
  7953. .pdev = pdev,
  7954. .notify_types = NOTIFY_DEVICEID4_CHANGE |
  7955. NOTIFY_DEVICEID4_DELETE,
  7956. };
  7957. struct nfs4_getdeviceinfo_res res = {
  7958. .pdev = pdev,
  7959. };
  7960. struct rpc_message msg = {
  7961. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  7962. .rpc_argp = &args,
  7963. .rpc_resp = &res,
  7964. .rpc_cred = cred,
  7965. };
  7966. int status;
  7967. dprintk("--> %s\n", __func__);
  7968. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  7969. if (res.notification & ~args.notify_types)
  7970. dprintk("%s: unsupported notification\n", __func__);
  7971. if (res.notification != args.notify_types)
  7972. pdev->nocache = 1;
  7973. dprintk("<-- %s status=%d\n", __func__, status);
  7974. return status;
  7975. }
  7976. int nfs4_proc_getdeviceinfo(struct nfs_server *server,
  7977. struct pnfs_device *pdev,
  7978. struct rpc_cred *cred)
  7979. {
  7980. struct nfs4_exception exception = { };
  7981. int err;
  7982. do {
  7983. err = nfs4_handle_exception(server,
  7984. _nfs4_proc_getdeviceinfo(server, pdev, cred),
  7985. &exception);
  7986. } while (exception.retry);
  7987. return err;
  7988. }
  7989. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  7990. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  7991. {
  7992. struct nfs4_layoutcommit_data *data = calldata;
  7993. struct nfs_server *server = NFS_SERVER(data->args.inode);
  7994. nfs4_setup_sequence(server->nfs_client,
  7995. &data->args.seq_args,
  7996. &data->res.seq_res,
  7997. task);
  7998. }
  7999. static void
  8000. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  8001. {
  8002. struct nfs4_layoutcommit_data *data = calldata;
  8003. struct nfs_server *server = NFS_SERVER(data->args.inode);
  8004. if (!nfs41_sequence_done(task, &data->res.seq_res))
  8005. return;
  8006. switch (task->tk_status) { /* Just ignore these failures */
  8007. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  8008. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  8009. case -NFS4ERR_BADLAYOUT: /* no layout */
  8010. case -NFS4ERR_GRACE: /* loca_recalim always false */
  8011. task->tk_status = 0;
  8012. case 0:
  8013. break;
  8014. default:
  8015. if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
  8016. rpc_restart_call_prepare(task);
  8017. return;
  8018. }
  8019. }
  8020. }
  8021. static void nfs4_layoutcommit_release(void *calldata)
  8022. {
  8023. struct nfs4_layoutcommit_data *data = calldata;
  8024. pnfs_cleanup_layoutcommit(data);
  8025. nfs_post_op_update_inode_force_wcc(data->args.inode,
  8026. data->res.fattr);
  8027. put_rpccred(data->cred);
  8028. nfs_iput_and_deactive(data->inode);
  8029. kfree(data);
  8030. }
  8031. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  8032. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  8033. .rpc_call_done = nfs4_layoutcommit_done,
  8034. .rpc_release = nfs4_layoutcommit_release,
  8035. };
  8036. int
  8037. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  8038. {
  8039. struct rpc_message msg = {
  8040. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  8041. .rpc_argp = &data->args,
  8042. .rpc_resp = &data->res,
  8043. .rpc_cred = data->cred,
  8044. };
  8045. struct rpc_task_setup task_setup_data = {
  8046. .task = &data->task,
  8047. .rpc_client = NFS_CLIENT(data->args.inode),
  8048. .rpc_message = &msg,
  8049. .callback_ops = &nfs4_layoutcommit_ops,
  8050. .callback_data = data,
  8051. };
  8052. struct rpc_task *task;
  8053. int status = 0;
  8054. dprintk("NFS: initiating layoutcommit call. sync %d "
  8055. "lbw: %llu inode %lu\n", sync,
  8056. data->args.lastbytewritten,
  8057. data->args.inode->i_ino);
  8058. if (!sync) {
  8059. data->inode = nfs_igrab_and_active(data->args.inode);
  8060. if (data->inode == NULL) {
  8061. nfs4_layoutcommit_release(data);
  8062. return -EAGAIN;
  8063. }
  8064. task_setup_data.flags = RPC_TASK_ASYNC;
  8065. }
  8066. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
  8067. task = rpc_run_task(&task_setup_data);
  8068. if (IS_ERR(task))
  8069. return PTR_ERR(task);
  8070. if (sync)
  8071. status = task->tk_status;
  8072. trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
  8073. dprintk("%s: status %d\n", __func__, status);
  8074. rpc_put_task(task);
  8075. return status;
  8076. }
  8077. /**
  8078. * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
  8079. * possible) as per RFC3530bis and RFC5661 Security Considerations sections
  8080. */
  8081. static int
  8082. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  8083. struct nfs_fsinfo *info,
  8084. struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  8085. {
  8086. struct nfs41_secinfo_no_name_args args = {
  8087. .style = SECINFO_STYLE_CURRENT_FH,
  8088. };
  8089. struct nfs4_secinfo_res res = {
  8090. .flavors = flavors,
  8091. };
  8092. struct rpc_message msg = {
  8093. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  8094. .rpc_argp = &args,
  8095. .rpc_resp = &res,
  8096. };
  8097. struct rpc_clnt *clnt = server->client;
  8098. struct rpc_cred *cred = NULL;
  8099. int status;
  8100. if (use_integrity) {
  8101. clnt = server->nfs_client->cl_rpcclient;
  8102. cred = nfs4_get_clid_cred(server->nfs_client);
  8103. msg.rpc_cred = cred;
  8104. }
  8105. dprintk("--> %s\n", __func__);
  8106. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
  8107. &res.seq_res, 0);
  8108. dprintk("<-- %s status=%d\n", __func__, status);
  8109. if (cred)
  8110. put_rpccred(cred);
  8111. return status;
  8112. }
  8113. static int
  8114. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  8115. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  8116. {
  8117. struct nfs4_exception exception = { };
  8118. int err;
  8119. do {
  8120. /* first try using integrity protection */
  8121. err = -NFS4ERR_WRONGSEC;
  8122. /* try to use integrity protection with machine cred */
  8123. if (_nfs4_is_integrity_protected(server->nfs_client))
  8124. err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
  8125. flavors, true);
  8126. /*
  8127. * if unable to use integrity protection, or SECINFO with
  8128. * integrity protection returns NFS4ERR_WRONGSEC (which is
  8129. * disallowed by spec, but exists in deployed servers) use
  8130. * the current filesystem's rpc_client and the user cred.
  8131. */
  8132. if (err == -NFS4ERR_WRONGSEC)
  8133. err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
  8134. flavors, false);
  8135. switch (err) {
  8136. case 0:
  8137. case -NFS4ERR_WRONGSEC:
  8138. case -ENOTSUPP:
  8139. goto out;
  8140. default:
  8141. err = nfs4_handle_exception(server, err, &exception);
  8142. }
  8143. } while (exception.retry);
  8144. out:
  8145. return err;
  8146. }
  8147. static int
  8148. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  8149. struct nfs_fsinfo *info)
  8150. {
  8151. int err;
  8152. struct page *page;
  8153. rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
  8154. struct nfs4_secinfo_flavors *flavors;
  8155. struct nfs4_secinfo4 *secinfo;
  8156. int i;
  8157. page = alloc_page(GFP_KERNEL);
  8158. if (!page) {
  8159. err = -ENOMEM;
  8160. goto out;
  8161. }
  8162. flavors = page_address(page);
  8163. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  8164. /*
  8165. * Fall back on "guess and check" method if
  8166. * the server doesn't support SECINFO_NO_NAME
  8167. */
  8168. if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
  8169. err = nfs4_find_root_sec(server, fhandle, info);
  8170. goto out_freepage;
  8171. }
  8172. if (err)
  8173. goto out_freepage;
  8174. for (i = 0; i < flavors->num_flavors; i++) {
  8175. secinfo = &flavors->flavors[i];
  8176. switch (secinfo->flavor) {
  8177. case RPC_AUTH_NULL:
  8178. case RPC_AUTH_UNIX:
  8179. case RPC_AUTH_GSS:
  8180. flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  8181. &secinfo->flavor_info);
  8182. break;
  8183. default:
  8184. flavor = RPC_AUTH_MAXFLAVOR;
  8185. break;
  8186. }
  8187. if (!nfs_auth_info_match(&server->auth_info, flavor))
  8188. flavor = RPC_AUTH_MAXFLAVOR;
  8189. if (flavor != RPC_AUTH_MAXFLAVOR) {
  8190. err = nfs4_lookup_root_sec(server, fhandle,
  8191. info, flavor);
  8192. if (!err)
  8193. break;
  8194. }
  8195. }
  8196. if (flavor == RPC_AUTH_MAXFLAVOR)
  8197. err = -EPERM;
  8198. out_freepage:
  8199. put_page(page);
  8200. if (err == -EACCES)
  8201. return -EPERM;
  8202. out:
  8203. return err;
  8204. }
  8205. static int _nfs41_test_stateid(struct nfs_server *server,
  8206. nfs4_stateid *stateid,
  8207. struct rpc_cred *cred)
  8208. {
  8209. int status;
  8210. struct nfs41_test_stateid_args args = {
  8211. .stateid = stateid,
  8212. };
  8213. struct nfs41_test_stateid_res res;
  8214. struct rpc_message msg = {
  8215. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  8216. .rpc_argp = &args,
  8217. .rpc_resp = &res,
  8218. .rpc_cred = cred,
  8219. };
  8220. struct rpc_clnt *rpc_client = server->client;
  8221. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
  8222. &rpc_client, &msg);
  8223. dprintk("NFS call test_stateid %p\n", stateid);
  8224. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
  8225. status = nfs4_call_sync_sequence(rpc_client, server, &msg,
  8226. &args.seq_args, &res.seq_res);
  8227. if (status != NFS_OK) {
  8228. dprintk("NFS reply test_stateid: failed, %d\n", status);
  8229. return status;
  8230. }
  8231. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  8232. return -res.status;
  8233. }
  8234. static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
  8235. int err, struct nfs4_exception *exception)
  8236. {
  8237. exception->retry = 0;
  8238. switch(err) {
  8239. case -NFS4ERR_DELAY:
  8240. case -NFS4ERR_RETRY_UNCACHED_REP:
  8241. nfs4_handle_exception(server, err, exception);
  8242. break;
  8243. case -NFS4ERR_BADSESSION:
  8244. case -NFS4ERR_BADSLOT:
  8245. case -NFS4ERR_BAD_HIGH_SLOT:
  8246. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  8247. case -NFS4ERR_DEADSESSION:
  8248. nfs4_do_handle_exception(server, err, exception);
  8249. }
  8250. }
  8251. /**
  8252. * nfs41_test_stateid - perform a TEST_STATEID operation
  8253. *
  8254. * @server: server / transport on which to perform the operation
  8255. * @stateid: state ID to test
  8256. * @cred: credential
  8257. *
  8258. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  8259. * Otherwise a negative NFS4ERR value is returned if the operation
  8260. * failed or the state ID is not currently valid.
  8261. */
  8262. static int nfs41_test_stateid(struct nfs_server *server,
  8263. nfs4_stateid *stateid,
  8264. struct rpc_cred *cred)
  8265. {
  8266. struct nfs4_exception exception = { };
  8267. int err;
  8268. do {
  8269. err = _nfs41_test_stateid(server, stateid, cred);
  8270. nfs4_handle_delay_or_session_error(server, err, &exception);
  8271. } while (exception.retry);
  8272. return err;
  8273. }
  8274. struct nfs_free_stateid_data {
  8275. struct nfs_server *server;
  8276. struct nfs41_free_stateid_args args;
  8277. struct nfs41_free_stateid_res res;
  8278. };
  8279. static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
  8280. {
  8281. struct nfs_free_stateid_data *data = calldata;
  8282. nfs4_setup_sequence(data->server->nfs_client,
  8283. &data->args.seq_args,
  8284. &data->res.seq_res,
  8285. task);
  8286. }
  8287. static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
  8288. {
  8289. struct nfs_free_stateid_data *data = calldata;
  8290. nfs41_sequence_done(task, &data->res.seq_res);
  8291. switch (task->tk_status) {
  8292. case -NFS4ERR_DELAY:
  8293. if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
  8294. rpc_restart_call_prepare(task);
  8295. }
  8296. }
  8297. static void nfs41_free_stateid_release(void *calldata)
  8298. {
  8299. kfree(calldata);
  8300. }
  8301. static const struct rpc_call_ops nfs41_free_stateid_ops = {
  8302. .rpc_call_prepare = nfs41_free_stateid_prepare,
  8303. .rpc_call_done = nfs41_free_stateid_done,
  8304. .rpc_release = nfs41_free_stateid_release,
  8305. };
  8306. /**
  8307. * nfs41_free_stateid - perform a FREE_STATEID operation
  8308. *
  8309. * @server: server / transport on which to perform the operation
  8310. * @stateid: state ID to release
  8311. * @cred: credential
  8312. * @is_recovery: set to true if this call needs to be privileged
  8313. *
  8314. * Note: this function is always asynchronous.
  8315. */
  8316. static int nfs41_free_stateid(struct nfs_server *server,
  8317. const nfs4_stateid *stateid,
  8318. struct rpc_cred *cred,
  8319. bool privileged)
  8320. {
  8321. struct rpc_message msg = {
  8322. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  8323. .rpc_cred = cred,
  8324. };
  8325. struct rpc_task_setup task_setup = {
  8326. .rpc_client = server->client,
  8327. .rpc_message = &msg,
  8328. .callback_ops = &nfs41_free_stateid_ops,
  8329. .flags = RPC_TASK_ASYNC,
  8330. };
  8331. struct nfs_free_stateid_data *data;
  8332. struct rpc_task *task;
  8333. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
  8334. &task_setup.rpc_client, &msg);
  8335. dprintk("NFS call free_stateid %p\n", stateid);
  8336. data = kmalloc(sizeof(*data), GFP_NOFS);
  8337. if (!data)
  8338. return -ENOMEM;
  8339. data->server = server;
  8340. nfs4_stateid_copy(&data->args.stateid, stateid);
  8341. task_setup.callback_data = data;
  8342. msg.rpc_argp = &data->args;
  8343. msg.rpc_resp = &data->res;
  8344. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
  8345. task = rpc_run_task(&task_setup);
  8346. if (IS_ERR(task))
  8347. return PTR_ERR(task);
  8348. rpc_put_task(task);
  8349. return 0;
  8350. }
  8351. static void
  8352. nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  8353. {
  8354. struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
  8355. nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
  8356. nfs4_free_lock_state(server, lsp);
  8357. }
  8358. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  8359. const nfs4_stateid *s2)
  8360. {
  8361. if (s1->type != s2->type)
  8362. return false;
  8363. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  8364. return false;
  8365. if (s1->seqid == s2->seqid)
  8366. return true;
  8367. return s1->seqid == 0 || s2->seqid == 0;
  8368. }
  8369. #endif /* CONFIG_NFS_V4_1 */
  8370. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  8371. const nfs4_stateid *s2)
  8372. {
  8373. return nfs4_stateid_match(s1, s2);
  8374. }
  8375. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  8376. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  8377. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  8378. .recover_open = nfs4_open_reclaim,
  8379. .recover_lock = nfs4_lock_reclaim,
  8380. .establish_clid = nfs4_init_clientid,
  8381. .detect_trunking = nfs40_discover_server_trunking,
  8382. };
  8383. #if defined(CONFIG_NFS_V4_1)
  8384. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  8385. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  8386. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  8387. .recover_open = nfs4_open_reclaim,
  8388. .recover_lock = nfs4_lock_reclaim,
  8389. .establish_clid = nfs41_init_clientid,
  8390. .reclaim_complete = nfs41_proc_reclaim_complete,
  8391. .detect_trunking = nfs41_discover_server_trunking,
  8392. };
  8393. #endif /* CONFIG_NFS_V4_1 */
  8394. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  8395. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  8396. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  8397. .recover_open = nfs40_open_expired,
  8398. .recover_lock = nfs4_lock_expired,
  8399. .establish_clid = nfs4_init_clientid,
  8400. };
  8401. #if defined(CONFIG_NFS_V4_1)
  8402. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  8403. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  8404. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  8405. .recover_open = nfs41_open_expired,
  8406. .recover_lock = nfs41_lock_expired,
  8407. .establish_clid = nfs41_init_clientid,
  8408. };
  8409. #endif /* CONFIG_NFS_V4_1 */
  8410. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  8411. .sched_state_renewal = nfs4_proc_async_renew,
  8412. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  8413. .renew_lease = nfs4_proc_renew,
  8414. };
  8415. #if defined(CONFIG_NFS_V4_1)
  8416. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  8417. .sched_state_renewal = nfs41_proc_async_sequence,
  8418. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  8419. .renew_lease = nfs4_proc_sequence,
  8420. };
  8421. #endif
  8422. static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
  8423. .get_locations = _nfs40_proc_get_locations,
  8424. .fsid_present = _nfs40_proc_fsid_present,
  8425. };
  8426. #if defined(CONFIG_NFS_V4_1)
  8427. static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
  8428. .get_locations = _nfs41_proc_get_locations,
  8429. .fsid_present = _nfs41_proc_fsid_present,
  8430. };
  8431. #endif /* CONFIG_NFS_V4_1 */
  8432. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  8433. .minor_version = 0,
  8434. .init_caps = NFS_CAP_READDIRPLUS
  8435. | NFS_CAP_ATOMIC_OPEN
  8436. | NFS_CAP_POSIX_LOCK,
  8437. .init_client = nfs40_init_client,
  8438. .shutdown_client = nfs40_shutdown_client,
  8439. .match_stateid = nfs4_match_stateid,
  8440. .find_root_sec = nfs4_find_root_sec,
  8441. .free_lock_state = nfs4_release_lockowner,
  8442. .test_and_free_expired = nfs40_test_and_free_expired_stateid,
  8443. .alloc_seqid = nfs_alloc_seqid,
  8444. .call_sync_ops = &nfs40_call_sync_ops,
  8445. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  8446. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  8447. .state_renewal_ops = &nfs40_state_renewal_ops,
  8448. .mig_recovery_ops = &nfs40_mig_recovery_ops,
  8449. };
  8450. #if defined(CONFIG_NFS_V4_1)
  8451. static struct nfs_seqid *
  8452. nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
  8453. {
  8454. return NULL;
  8455. }
  8456. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  8457. .minor_version = 1,
  8458. .init_caps = NFS_CAP_READDIRPLUS
  8459. | NFS_CAP_ATOMIC_OPEN
  8460. | NFS_CAP_POSIX_LOCK
  8461. | NFS_CAP_STATEID_NFSV41
  8462. | NFS_CAP_ATOMIC_OPEN_V1
  8463. | NFS_CAP_LGOPEN,
  8464. .init_client = nfs41_init_client,
  8465. .shutdown_client = nfs41_shutdown_client,
  8466. .match_stateid = nfs41_match_stateid,
  8467. .find_root_sec = nfs41_find_root_sec,
  8468. .free_lock_state = nfs41_free_lock_state,
  8469. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  8470. .alloc_seqid = nfs_alloc_no_seqid,
  8471. .session_trunk = nfs4_test_session_trunk,
  8472. .call_sync_ops = &nfs41_call_sync_ops,
  8473. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  8474. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  8475. .state_renewal_ops = &nfs41_state_renewal_ops,
  8476. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  8477. };
  8478. #endif
  8479. #if defined(CONFIG_NFS_V4_2)
  8480. static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
  8481. .minor_version = 2,
  8482. .init_caps = NFS_CAP_READDIRPLUS
  8483. | NFS_CAP_ATOMIC_OPEN
  8484. | NFS_CAP_POSIX_LOCK
  8485. | NFS_CAP_STATEID_NFSV41
  8486. | NFS_CAP_ATOMIC_OPEN_V1
  8487. | NFS_CAP_LGOPEN
  8488. | NFS_CAP_ALLOCATE
  8489. | NFS_CAP_COPY
  8490. | NFS_CAP_DEALLOCATE
  8491. | NFS_CAP_SEEK
  8492. | NFS_CAP_LAYOUTSTATS
  8493. | NFS_CAP_CLONE,
  8494. .init_client = nfs41_init_client,
  8495. .shutdown_client = nfs41_shutdown_client,
  8496. .match_stateid = nfs41_match_stateid,
  8497. .find_root_sec = nfs41_find_root_sec,
  8498. .free_lock_state = nfs41_free_lock_state,
  8499. .call_sync_ops = &nfs41_call_sync_ops,
  8500. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  8501. .alloc_seqid = nfs_alloc_no_seqid,
  8502. .session_trunk = nfs4_test_session_trunk,
  8503. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  8504. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  8505. .state_renewal_ops = &nfs41_state_renewal_ops,
  8506. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  8507. };
  8508. #endif
  8509. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  8510. [0] = &nfs_v4_0_minor_ops,
  8511. #if defined(CONFIG_NFS_V4_1)
  8512. [1] = &nfs_v4_1_minor_ops,
  8513. #endif
  8514. #if defined(CONFIG_NFS_V4_2)
  8515. [2] = &nfs_v4_2_minor_ops,
  8516. #endif
  8517. };
  8518. static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
  8519. {
  8520. ssize_t error, error2;
  8521. error = generic_listxattr(dentry, list, size);
  8522. if (error < 0)
  8523. return error;
  8524. if (list) {
  8525. list += error;
  8526. size -= error;
  8527. }
  8528. error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
  8529. if (error2 < 0)
  8530. return error2;
  8531. return error + error2;
  8532. }
  8533. static const struct inode_operations nfs4_dir_inode_operations = {
  8534. .create = nfs_create,
  8535. .lookup = nfs_lookup,
  8536. .atomic_open = nfs_atomic_open,
  8537. .link = nfs_link,
  8538. .unlink = nfs_unlink,
  8539. .symlink = nfs_symlink,
  8540. .mkdir = nfs_mkdir,
  8541. .rmdir = nfs_rmdir,
  8542. .mknod = nfs_mknod,
  8543. .rename = nfs_rename,
  8544. .permission = nfs_permission,
  8545. .getattr = nfs_getattr,
  8546. .setattr = nfs_setattr,
  8547. .listxattr = nfs4_listxattr,
  8548. };
  8549. static const struct inode_operations nfs4_file_inode_operations = {
  8550. .permission = nfs_permission,
  8551. .getattr = nfs_getattr,
  8552. .setattr = nfs_setattr,
  8553. .listxattr = nfs4_listxattr,
  8554. };
  8555. const struct nfs_rpc_ops nfs_v4_clientops = {
  8556. .version = 4, /* protocol version */
  8557. .dentry_ops = &nfs4_dentry_operations,
  8558. .dir_inode_ops = &nfs4_dir_inode_operations,
  8559. .file_inode_ops = &nfs4_file_inode_operations,
  8560. .file_ops = &nfs4_file_operations,
  8561. .getroot = nfs4_proc_get_root,
  8562. .submount = nfs4_submount,
  8563. .try_mount = nfs4_try_mount,
  8564. .getattr = nfs4_proc_getattr,
  8565. .setattr = nfs4_proc_setattr,
  8566. .lookup = nfs4_proc_lookup,
  8567. .lookupp = nfs4_proc_lookupp,
  8568. .access = nfs4_proc_access,
  8569. .readlink = nfs4_proc_readlink,
  8570. .create = nfs4_proc_create,
  8571. .remove = nfs4_proc_remove,
  8572. .unlink_setup = nfs4_proc_unlink_setup,
  8573. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  8574. .unlink_done = nfs4_proc_unlink_done,
  8575. .rename_setup = nfs4_proc_rename_setup,
  8576. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  8577. .rename_done = nfs4_proc_rename_done,
  8578. .link = nfs4_proc_link,
  8579. .symlink = nfs4_proc_symlink,
  8580. .mkdir = nfs4_proc_mkdir,
  8581. .rmdir = nfs4_proc_rmdir,
  8582. .readdir = nfs4_proc_readdir,
  8583. .mknod = nfs4_proc_mknod,
  8584. .statfs = nfs4_proc_statfs,
  8585. .fsinfo = nfs4_proc_fsinfo,
  8586. .pathconf = nfs4_proc_pathconf,
  8587. .set_capabilities = nfs4_server_capabilities,
  8588. .decode_dirent = nfs4_decode_dirent,
  8589. .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
  8590. .read_setup = nfs4_proc_read_setup,
  8591. .read_done = nfs4_read_done,
  8592. .write_setup = nfs4_proc_write_setup,
  8593. .write_done = nfs4_write_done,
  8594. .commit_setup = nfs4_proc_commit_setup,
  8595. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  8596. .commit_done = nfs4_commit_done,
  8597. .lock = nfs4_proc_lock,
  8598. .clear_acl_cache = nfs4_zap_acl_attr,
  8599. .close_context = nfs4_close_context,
  8600. .open_context = nfs4_atomic_open,
  8601. .have_delegation = nfs4_have_delegation,
  8602. .alloc_client = nfs4_alloc_client,
  8603. .init_client = nfs4_init_client,
  8604. .free_client = nfs4_free_client,
  8605. .create_server = nfs4_create_server,
  8606. .clone_server = nfs_clone_server,
  8607. };
  8608. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  8609. .name = XATTR_NAME_NFSV4_ACL,
  8610. .list = nfs4_xattr_list_nfs4_acl,
  8611. .get = nfs4_xattr_get_nfs4_acl,
  8612. .set = nfs4_xattr_set_nfs4_acl,
  8613. };
  8614. const struct xattr_handler *nfs4_xattr_handlers[] = {
  8615. &nfs4_xattr_nfs4_acl_handler,
  8616. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  8617. &nfs4_xattr_nfs4_label_handler,
  8618. #endif
  8619. NULL
  8620. };
  8621. /*
  8622. * Local variables:
  8623. * c-basic-offset: 8
  8624. * End:
  8625. */