nfs4proc.c 258 KB

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