nfs4proc.c 258 KB

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