nfs4proc.c 234 KB

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