nfs4proc.c 263 KB

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