nfs4proc.c 236 KB

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