nfs4proc.c 295 KB

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