nfs4proc.c 234 KB

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