nfs4proc.c 236 KB

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