nfs4proc.c 258 KB

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