nfs4proc.c 254 KB

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