nfs4proc.c 240 KB

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