nfs4proc.c 279 KB

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