nfs4proc.c 237 KB

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