nfs4proc.c 253 KB

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