nfs4proc.c 236 KB

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