nfs4proc.c 257 KB

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