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