nfs4proc.c 263 KB

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