nfs4proc.c 253 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431
  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. sattr->ia_valid &= ~ATTR_MODE;
  2348. if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
  2349. *label = NULL;
  2350. }
  2351. static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  2352. fmode_t fmode,
  2353. int flags,
  2354. struct nfs_open_context *ctx)
  2355. {
  2356. struct nfs4_state_owner *sp = opendata->owner;
  2357. struct nfs_server *server = sp->so_server;
  2358. struct dentry *dentry;
  2359. struct nfs4_state *state;
  2360. unsigned int seq;
  2361. int ret;
  2362. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  2363. ret = _nfs4_proc_open(opendata);
  2364. if (ret != 0)
  2365. goto out;
  2366. state = nfs4_opendata_to_nfs4_state(opendata);
  2367. ret = PTR_ERR(state);
  2368. if (IS_ERR(state))
  2369. goto out;
  2370. ctx->state = state;
  2371. if (server->caps & NFS_CAP_POSIX_LOCK)
  2372. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  2373. if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
  2374. set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
  2375. dentry = opendata->dentry;
  2376. if (d_really_is_negative(dentry)) {
  2377. struct dentry *alias;
  2378. d_drop(dentry);
  2379. alias = d_exact_alias(dentry, state->inode);
  2380. if (!alias)
  2381. alias = d_splice_alias(igrab(state->inode), dentry);
  2382. /* d_splice_alias() can't fail here - it's a non-directory */
  2383. if (alias) {
  2384. dput(ctx->dentry);
  2385. ctx->dentry = dentry = alias;
  2386. }
  2387. nfs_set_verifier(dentry,
  2388. nfs_save_change_attribute(d_inode(opendata->dir)));
  2389. }
  2390. ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
  2391. if (ret != 0)
  2392. goto out;
  2393. if (d_inode(dentry) == state->inode) {
  2394. nfs_inode_attach_open_context(ctx);
  2395. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  2396. nfs4_schedule_stateid_recovery(server, state);
  2397. }
  2398. out:
  2399. return ret;
  2400. }
  2401. /*
  2402. * Returns a referenced nfs4_state
  2403. */
  2404. static int _nfs4_do_open(struct inode *dir,
  2405. struct nfs_open_context *ctx,
  2406. int flags,
  2407. struct iattr *sattr,
  2408. struct nfs4_label *label,
  2409. int *opened)
  2410. {
  2411. struct nfs4_state_owner *sp;
  2412. struct nfs4_state *state = NULL;
  2413. struct nfs_server *server = NFS_SERVER(dir);
  2414. struct nfs4_opendata *opendata;
  2415. struct dentry *dentry = ctx->dentry;
  2416. struct rpc_cred *cred = ctx->cred;
  2417. struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
  2418. fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
  2419. enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
  2420. struct nfs4_label *olabel = NULL;
  2421. int status;
  2422. /* Protect against reboot recovery conflicts */
  2423. status = -ENOMEM;
  2424. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  2425. if (sp == NULL) {
  2426. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  2427. goto out_err;
  2428. }
  2429. status = nfs4_client_recover_expired_lease(server->nfs_client);
  2430. if (status != 0)
  2431. goto err_put_state_owner;
  2432. if (d_really_is_positive(dentry))
  2433. nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
  2434. status = -ENOMEM;
  2435. if (d_really_is_positive(dentry))
  2436. claim = NFS4_OPEN_CLAIM_FH;
  2437. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
  2438. label, claim, GFP_KERNEL);
  2439. if (opendata == NULL)
  2440. goto err_put_state_owner;
  2441. if (label) {
  2442. olabel = nfs4_label_alloc(server, GFP_KERNEL);
  2443. if (IS_ERR(olabel)) {
  2444. status = PTR_ERR(olabel);
  2445. goto err_opendata_put;
  2446. }
  2447. }
  2448. if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  2449. if (!opendata->f_attr.mdsthreshold) {
  2450. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  2451. if (!opendata->f_attr.mdsthreshold)
  2452. goto err_free_label;
  2453. }
  2454. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  2455. }
  2456. if (d_really_is_positive(dentry))
  2457. opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
  2458. status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
  2459. if (status != 0)
  2460. goto err_free_label;
  2461. state = ctx->state;
  2462. if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
  2463. (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
  2464. nfs4_exclusive_attrset(opendata, sattr, &label);
  2465. /*
  2466. * send create attributes which was not set by open
  2467. * with an extra setattr.
  2468. */
  2469. if (sattr->ia_valid & NFS4_VALID_ATTRS) {
  2470. nfs_fattr_init(opendata->o_res.f_attr);
  2471. status = nfs4_do_setattr(state->inode, cred,
  2472. opendata->o_res.f_attr, sattr,
  2473. ctx, label, olabel);
  2474. if (status == 0) {
  2475. nfs_setattr_update_inode(state->inode, sattr,
  2476. opendata->o_res.f_attr);
  2477. nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
  2478. }
  2479. }
  2480. }
  2481. if (opened && opendata->file_created)
  2482. *opened |= FILE_CREATED;
  2483. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
  2484. *ctx_th = opendata->f_attr.mdsthreshold;
  2485. opendata->f_attr.mdsthreshold = NULL;
  2486. }
  2487. nfs4_label_free(olabel);
  2488. nfs4_opendata_put(opendata);
  2489. nfs4_put_state_owner(sp);
  2490. return 0;
  2491. err_free_label:
  2492. nfs4_label_free(olabel);
  2493. err_opendata_put:
  2494. nfs4_opendata_put(opendata);
  2495. err_put_state_owner:
  2496. nfs4_put_state_owner(sp);
  2497. out_err:
  2498. return status;
  2499. }
  2500. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  2501. struct nfs_open_context *ctx,
  2502. int flags,
  2503. struct iattr *sattr,
  2504. struct nfs4_label *label,
  2505. int *opened)
  2506. {
  2507. struct nfs_server *server = NFS_SERVER(dir);
  2508. struct nfs4_exception exception = { };
  2509. struct nfs4_state *res;
  2510. int status;
  2511. do {
  2512. status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
  2513. res = ctx->state;
  2514. trace_nfs4_open_file(ctx, flags, status);
  2515. if (status == 0)
  2516. break;
  2517. /* NOTE: BAD_SEQID means the server and client disagree about the
  2518. * book-keeping w.r.t. state-changing operations
  2519. * (OPEN/CLOSE/LOCK/LOCKU...)
  2520. * It is actually a sign of a bug on the client or on the server.
  2521. *
  2522. * If we receive a BAD_SEQID error in the particular case of
  2523. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  2524. * have unhashed the old state_owner for us, and that we can
  2525. * therefore safely retry using a new one. We should still warn
  2526. * the user though...
  2527. */
  2528. if (status == -NFS4ERR_BAD_SEQID) {
  2529. pr_warn_ratelimited("NFS: v4 server %s "
  2530. " returned a bad sequence-id error!\n",
  2531. NFS_SERVER(dir)->nfs_client->cl_hostname);
  2532. exception.retry = 1;
  2533. continue;
  2534. }
  2535. /*
  2536. * BAD_STATEID on OPEN means that the server cancelled our
  2537. * state before it received the OPEN_CONFIRM.
  2538. * Recover by retrying the request as per the discussion
  2539. * on Page 181 of RFC3530.
  2540. */
  2541. if (status == -NFS4ERR_BAD_STATEID) {
  2542. exception.retry = 1;
  2543. continue;
  2544. }
  2545. if (status == -EAGAIN) {
  2546. /* We must have found a delegation */
  2547. exception.retry = 1;
  2548. continue;
  2549. }
  2550. if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
  2551. continue;
  2552. res = ERR_PTR(nfs4_handle_exception(server,
  2553. status, &exception));
  2554. } while (exception.retry);
  2555. return res;
  2556. }
  2557. static int _nfs4_do_setattr(struct inode *inode,
  2558. struct nfs_setattrargs *arg,
  2559. struct nfs_setattrres *res,
  2560. struct rpc_cred *cred,
  2561. struct nfs_open_context *ctx)
  2562. {
  2563. struct nfs_server *server = NFS_SERVER(inode);
  2564. struct rpc_message msg = {
  2565. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  2566. .rpc_argp = arg,
  2567. .rpc_resp = res,
  2568. .rpc_cred = cred,
  2569. };
  2570. struct rpc_cred *delegation_cred = NULL;
  2571. unsigned long timestamp = jiffies;
  2572. fmode_t fmode;
  2573. bool truncate;
  2574. int status;
  2575. nfs_fattr_init(res->fattr);
  2576. /* Servers should only apply open mode checks for file size changes */
  2577. truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
  2578. fmode = truncate ? FMODE_WRITE : FMODE_READ;
  2579. if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
  2580. /* Use that stateid */
  2581. } else if (truncate && ctx != NULL) {
  2582. struct nfs_lock_context *l_ctx;
  2583. if (!nfs4_valid_open_stateid(ctx->state))
  2584. return -EBADF;
  2585. l_ctx = nfs_get_lock_context(ctx);
  2586. if (IS_ERR(l_ctx))
  2587. return PTR_ERR(l_ctx);
  2588. status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
  2589. &arg->stateid, &delegation_cred);
  2590. nfs_put_lock_context(l_ctx);
  2591. if (status == -EIO)
  2592. return -EBADF;
  2593. } else
  2594. nfs4_stateid_copy(&arg->stateid, &zero_stateid);
  2595. if (delegation_cred)
  2596. msg.rpc_cred = delegation_cred;
  2597. status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
  2598. put_rpccred(delegation_cred);
  2599. if (status == 0 && ctx != NULL)
  2600. renew_lease(server, timestamp);
  2601. trace_nfs4_setattr(inode, &arg->stateid, status);
  2602. return status;
  2603. }
  2604. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  2605. struct nfs_fattr *fattr, struct iattr *sattr,
  2606. struct nfs_open_context *ctx, struct nfs4_label *ilabel,
  2607. struct nfs4_label *olabel)
  2608. {
  2609. struct nfs_server *server = NFS_SERVER(inode);
  2610. struct nfs4_state *state = ctx ? ctx->state : NULL;
  2611. struct nfs_setattrargs arg = {
  2612. .fh = NFS_FH(inode),
  2613. .iap = sattr,
  2614. .server = server,
  2615. .bitmask = server->attr_bitmask,
  2616. .label = ilabel,
  2617. };
  2618. struct nfs_setattrres res = {
  2619. .fattr = fattr,
  2620. .label = olabel,
  2621. .server = server,
  2622. };
  2623. struct nfs4_exception exception = {
  2624. .state = state,
  2625. .inode = inode,
  2626. .stateid = &arg.stateid,
  2627. };
  2628. int err;
  2629. arg.bitmask = nfs4_bitmask(server, ilabel);
  2630. if (ilabel)
  2631. arg.bitmask = nfs4_bitmask(server, olabel);
  2632. do {
  2633. err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
  2634. switch (err) {
  2635. case -NFS4ERR_OPENMODE:
  2636. if (!(sattr->ia_valid & ATTR_SIZE)) {
  2637. pr_warn_once("NFSv4: server %s is incorrectly "
  2638. "applying open mode checks to "
  2639. "a SETATTR that is not "
  2640. "changing file size.\n",
  2641. server->nfs_client->cl_hostname);
  2642. }
  2643. if (state && !(state->state & FMODE_WRITE)) {
  2644. err = -EBADF;
  2645. if (sattr->ia_valid & ATTR_OPEN)
  2646. err = -EACCES;
  2647. goto out;
  2648. }
  2649. }
  2650. err = nfs4_handle_exception(server, err, &exception);
  2651. } while (exception.retry);
  2652. out:
  2653. return err;
  2654. }
  2655. static bool
  2656. nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
  2657. {
  2658. if (inode == NULL || !nfs_have_layout(inode))
  2659. return false;
  2660. return pnfs_wait_on_layoutreturn(inode, task);
  2661. }
  2662. struct nfs4_closedata {
  2663. struct inode *inode;
  2664. struct nfs4_state *state;
  2665. struct nfs_closeargs arg;
  2666. struct nfs_closeres res;
  2667. struct {
  2668. struct nfs4_layoutreturn_args arg;
  2669. struct nfs4_layoutreturn_res res;
  2670. struct nfs4_xdr_opaque_data ld_private;
  2671. u32 roc_barrier;
  2672. bool roc;
  2673. } lr;
  2674. struct nfs_fattr fattr;
  2675. unsigned long timestamp;
  2676. };
  2677. static void nfs4_free_closedata(void *data)
  2678. {
  2679. struct nfs4_closedata *calldata = data;
  2680. struct nfs4_state_owner *sp = calldata->state->owner;
  2681. struct super_block *sb = calldata->state->inode->i_sb;
  2682. if (calldata->lr.roc)
  2683. pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
  2684. calldata->res.lr_ret);
  2685. nfs4_put_open_state(calldata->state);
  2686. nfs_free_seqid(calldata->arg.seqid);
  2687. nfs4_put_state_owner(sp);
  2688. nfs_sb_deactive(sb);
  2689. kfree(calldata);
  2690. }
  2691. static void nfs4_close_done(struct rpc_task *task, void *data)
  2692. {
  2693. struct nfs4_closedata *calldata = data;
  2694. struct nfs4_state *state = calldata->state;
  2695. struct nfs_server *server = NFS_SERVER(calldata->inode);
  2696. nfs4_stateid *res_stateid = NULL;
  2697. dprintk("%s: begin!\n", __func__);
  2698. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  2699. return;
  2700. trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
  2701. /* Handle Layoutreturn errors */
  2702. if (calldata->arg.lr_args && task->tk_status != 0) {
  2703. switch (calldata->res.lr_ret) {
  2704. default:
  2705. calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  2706. break;
  2707. case 0:
  2708. calldata->arg.lr_args = NULL;
  2709. calldata->res.lr_res = NULL;
  2710. break;
  2711. case -NFS4ERR_ADMIN_REVOKED:
  2712. case -NFS4ERR_DELEG_REVOKED:
  2713. case -NFS4ERR_EXPIRED:
  2714. case -NFS4ERR_BAD_STATEID:
  2715. case -NFS4ERR_OLD_STATEID:
  2716. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  2717. case -NFS4ERR_WRONG_CRED:
  2718. calldata->arg.lr_args = NULL;
  2719. calldata->res.lr_res = NULL;
  2720. calldata->res.lr_ret = 0;
  2721. rpc_restart_call_prepare(task);
  2722. return;
  2723. }
  2724. }
  2725. /* hmm. we are done with the inode, and in the process of freeing
  2726. * the state_owner. we keep this around to process errors
  2727. */
  2728. switch (task->tk_status) {
  2729. case 0:
  2730. res_stateid = &calldata->res.stateid;
  2731. renew_lease(server, calldata->timestamp);
  2732. break;
  2733. case -NFS4ERR_ACCESS:
  2734. if (calldata->arg.bitmask != NULL) {
  2735. calldata->arg.bitmask = NULL;
  2736. calldata->res.fattr = NULL;
  2737. task->tk_status = 0;
  2738. rpc_restart_call_prepare(task);
  2739. goto out_release;
  2740. }
  2741. break;
  2742. case -NFS4ERR_ADMIN_REVOKED:
  2743. case -NFS4ERR_STALE_STATEID:
  2744. case -NFS4ERR_EXPIRED:
  2745. nfs4_free_revoked_stateid(server,
  2746. &calldata->arg.stateid,
  2747. task->tk_msg.rpc_cred);
  2748. case -NFS4ERR_OLD_STATEID:
  2749. case -NFS4ERR_BAD_STATEID:
  2750. if (!nfs4_stateid_match(&calldata->arg.stateid,
  2751. &state->open_stateid)) {
  2752. rpc_restart_call_prepare(task);
  2753. goto out_release;
  2754. }
  2755. if (calldata->arg.fmode == 0)
  2756. break;
  2757. default:
  2758. if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
  2759. rpc_restart_call_prepare(task);
  2760. goto out_release;
  2761. }
  2762. }
  2763. nfs_clear_open_stateid(state, &calldata->arg.stateid,
  2764. res_stateid, calldata->arg.fmode);
  2765. out_release:
  2766. nfs_release_seqid(calldata->arg.seqid);
  2767. nfs_refresh_inode(calldata->inode, &calldata->fattr);
  2768. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  2769. }
  2770. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  2771. {
  2772. struct nfs4_closedata *calldata = data;
  2773. struct nfs4_state *state = calldata->state;
  2774. struct inode *inode = calldata->inode;
  2775. bool is_rdonly, is_wronly, is_rdwr;
  2776. int call_close = 0;
  2777. dprintk("%s: begin!\n", __func__);
  2778. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  2779. goto out_wait;
  2780. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  2781. spin_lock(&state->owner->so_lock);
  2782. is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
  2783. is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
  2784. is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
  2785. nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
  2786. /* Calculate the change in open mode */
  2787. calldata->arg.fmode = 0;
  2788. if (state->n_rdwr == 0) {
  2789. if (state->n_rdonly == 0)
  2790. call_close |= is_rdonly;
  2791. else if (is_rdonly)
  2792. calldata->arg.fmode |= FMODE_READ;
  2793. if (state->n_wronly == 0)
  2794. call_close |= is_wronly;
  2795. else if (is_wronly)
  2796. calldata->arg.fmode |= FMODE_WRITE;
  2797. if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
  2798. call_close |= is_rdwr;
  2799. } else if (is_rdwr)
  2800. calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
  2801. if (!nfs4_valid_open_stateid(state) ||
  2802. test_bit(NFS_OPEN_STATE, &state->flags) == 0)
  2803. call_close = 0;
  2804. spin_unlock(&state->owner->so_lock);
  2805. if (!call_close) {
  2806. /* Note: exit _without_ calling nfs4_close_done */
  2807. goto out_no_action;
  2808. }
  2809. if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
  2810. nfs_release_seqid(calldata->arg.seqid);
  2811. goto out_wait;
  2812. }
  2813. if (calldata->arg.fmode == 0)
  2814. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  2815. if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
  2816. /* Close-to-open cache consistency revalidation */
  2817. if (!nfs4_have_delegation(inode, FMODE_READ))
  2818. calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
  2819. else
  2820. calldata->arg.bitmask = NULL;
  2821. }
  2822. calldata->arg.share_access =
  2823. nfs4_map_atomic_open_share(NFS_SERVER(inode),
  2824. calldata->arg.fmode, 0);
  2825. if (calldata->res.fattr == NULL)
  2826. calldata->arg.bitmask = NULL;
  2827. else if (calldata->arg.bitmask == NULL)
  2828. calldata->res.fattr = NULL;
  2829. calldata->timestamp = jiffies;
  2830. if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
  2831. &calldata->arg.seq_args,
  2832. &calldata->res.seq_res,
  2833. task) != 0)
  2834. nfs_release_seqid(calldata->arg.seqid);
  2835. dprintk("%s: done!\n", __func__);
  2836. return;
  2837. out_no_action:
  2838. task->tk_action = NULL;
  2839. out_wait:
  2840. nfs4_sequence_done(task, &calldata->res.seq_res);
  2841. }
  2842. static const struct rpc_call_ops nfs4_close_ops = {
  2843. .rpc_call_prepare = nfs4_close_prepare,
  2844. .rpc_call_done = nfs4_close_done,
  2845. .rpc_release = nfs4_free_closedata,
  2846. };
  2847. /*
  2848. * It is possible for data to be read/written from a mem-mapped file
  2849. * after the sys_close call (which hits the vfs layer as a flush).
  2850. * This means that we can't safely call nfsv4 close on a file until
  2851. * the inode is cleared. This in turn means that we are not good
  2852. * NFSv4 citizens - we do not indicate to the server to update the file's
  2853. * share state even when we are done with one of the three share
  2854. * stateid's in the inode.
  2855. *
  2856. * NOTE: Caller must be holding the sp->so_owner semaphore!
  2857. */
  2858. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  2859. {
  2860. struct nfs_server *server = NFS_SERVER(state->inode);
  2861. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  2862. struct nfs4_closedata *calldata;
  2863. struct nfs4_state_owner *sp = state->owner;
  2864. struct rpc_task *task;
  2865. struct rpc_message msg = {
  2866. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  2867. .rpc_cred = state->owner->so_cred,
  2868. };
  2869. struct rpc_task_setup task_setup_data = {
  2870. .rpc_client = server->client,
  2871. .rpc_message = &msg,
  2872. .callback_ops = &nfs4_close_ops,
  2873. .workqueue = nfsiod_workqueue,
  2874. .flags = RPC_TASK_ASYNC,
  2875. };
  2876. int status = -ENOMEM;
  2877. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
  2878. &task_setup_data.rpc_client, &msg);
  2879. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  2880. if (calldata == NULL)
  2881. goto out;
  2882. nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
  2883. calldata->inode = state->inode;
  2884. calldata->state = state;
  2885. calldata->arg.fh = NFS_FH(state->inode);
  2886. /* Serialization for the sequence id */
  2887. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  2888. calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
  2889. if (IS_ERR(calldata->arg.seqid))
  2890. goto out_free_calldata;
  2891. nfs_fattr_init(&calldata->fattr);
  2892. calldata->arg.fmode = 0;
  2893. calldata->lr.arg.ld_private = &calldata->lr.ld_private;
  2894. calldata->res.fattr = &calldata->fattr;
  2895. calldata->res.seqid = calldata->arg.seqid;
  2896. calldata->res.server = server;
  2897. calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  2898. calldata->lr.roc = pnfs_roc(state->inode,
  2899. &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
  2900. if (calldata->lr.roc) {
  2901. calldata->arg.lr_args = &calldata->lr.arg;
  2902. calldata->res.lr_res = &calldata->lr.res;
  2903. }
  2904. nfs_sb_active(calldata->inode->i_sb);
  2905. msg.rpc_argp = &calldata->arg;
  2906. msg.rpc_resp = &calldata->res;
  2907. task_setup_data.callback_data = calldata;
  2908. task = rpc_run_task(&task_setup_data);
  2909. if (IS_ERR(task))
  2910. return PTR_ERR(task);
  2911. status = 0;
  2912. if (wait)
  2913. status = rpc_wait_for_completion_task(task);
  2914. rpc_put_task(task);
  2915. return status;
  2916. out_free_calldata:
  2917. kfree(calldata);
  2918. out:
  2919. nfs4_put_open_state(state);
  2920. nfs4_put_state_owner(sp);
  2921. return status;
  2922. }
  2923. static struct inode *
  2924. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
  2925. int open_flags, struct iattr *attr, int *opened)
  2926. {
  2927. struct nfs4_state *state;
  2928. struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
  2929. label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
  2930. /* Protect against concurrent sillydeletes */
  2931. state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
  2932. nfs4_label_release_security(label);
  2933. if (IS_ERR(state))
  2934. return ERR_CAST(state);
  2935. return state->inode;
  2936. }
  2937. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  2938. {
  2939. if (ctx->state == NULL)
  2940. return;
  2941. if (is_sync)
  2942. nfs4_close_sync(ctx->state, ctx->mode);
  2943. else
  2944. nfs4_close_state(ctx->state, ctx->mode);
  2945. }
  2946. #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
  2947. #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
  2948. #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
  2949. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2950. {
  2951. u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
  2952. struct nfs4_server_caps_arg args = {
  2953. .fhandle = fhandle,
  2954. .bitmask = bitmask,
  2955. };
  2956. struct nfs4_server_caps_res res = {};
  2957. struct rpc_message msg = {
  2958. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  2959. .rpc_argp = &args,
  2960. .rpc_resp = &res,
  2961. };
  2962. int status;
  2963. int i;
  2964. bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
  2965. FATTR4_WORD0_FH_EXPIRE_TYPE |
  2966. FATTR4_WORD0_LINK_SUPPORT |
  2967. FATTR4_WORD0_SYMLINK_SUPPORT |
  2968. FATTR4_WORD0_ACLSUPPORT;
  2969. if (minorversion)
  2970. bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
  2971. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2972. if (status == 0) {
  2973. /* Sanity check the server answers */
  2974. switch (minorversion) {
  2975. case 0:
  2976. res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
  2977. res.attr_bitmask[2] = 0;
  2978. break;
  2979. case 1:
  2980. res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
  2981. break;
  2982. case 2:
  2983. res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
  2984. }
  2985. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  2986. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  2987. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  2988. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  2989. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  2990. NFS_CAP_CTIME|NFS_CAP_MTIME|
  2991. NFS_CAP_SECURITY_LABEL);
  2992. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
  2993. res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2994. server->caps |= NFS_CAP_ACLS;
  2995. if (res.has_links != 0)
  2996. server->caps |= NFS_CAP_HARDLINKS;
  2997. if (res.has_symlinks != 0)
  2998. server->caps |= NFS_CAP_SYMLINKS;
  2999. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  3000. server->caps |= NFS_CAP_FILEID;
  3001. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  3002. server->caps |= NFS_CAP_MODE;
  3003. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  3004. server->caps |= NFS_CAP_NLINK;
  3005. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  3006. server->caps |= NFS_CAP_OWNER;
  3007. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  3008. server->caps |= NFS_CAP_OWNER_GROUP;
  3009. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  3010. server->caps |= NFS_CAP_ATIME;
  3011. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  3012. server->caps |= NFS_CAP_CTIME;
  3013. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  3014. server->caps |= NFS_CAP_MTIME;
  3015. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  3016. if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
  3017. server->caps |= NFS_CAP_SECURITY_LABEL;
  3018. #endif
  3019. memcpy(server->attr_bitmask_nl, res.attr_bitmask,
  3020. sizeof(server->attr_bitmask));
  3021. server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  3022. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  3023. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  3024. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  3025. server->cache_consistency_bitmask[2] = 0;
  3026. /* Avoid a regression due to buggy server */
  3027. for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
  3028. res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
  3029. memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
  3030. sizeof(server->exclcreat_bitmask));
  3031. server->acl_bitmask = res.acl_bitmask;
  3032. server->fh_expire_type = res.fh_expire_type;
  3033. }
  3034. return status;
  3035. }
  3036. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  3037. {
  3038. struct nfs4_exception exception = { };
  3039. int err;
  3040. do {
  3041. err = nfs4_handle_exception(server,
  3042. _nfs4_server_capabilities(server, fhandle),
  3043. &exception);
  3044. } while (exception.retry);
  3045. return err;
  3046. }
  3047. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3048. struct nfs_fsinfo *info)
  3049. {
  3050. u32 bitmask[3];
  3051. struct nfs4_lookup_root_arg args = {
  3052. .bitmask = bitmask,
  3053. };
  3054. struct nfs4_lookup_res res = {
  3055. .server = server,
  3056. .fattr = info->fattr,
  3057. .fh = fhandle,
  3058. };
  3059. struct rpc_message msg = {
  3060. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  3061. .rpc_argp = &args,
  3062. .rpc_resp = &res,
  3063. };
  3064. bitmask[0] = nfs4_fattr_bitmap[0];
  3065. bitmask[1] = nfs4_fattr_bitmap[1];
  3066. /*
  3067. * Process the label in the upcoming getfattr
  3068. */
  3069. bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
  3070. nfs_fattr_init(info->fattr);
  3071. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3072. }
  3073. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3074. struct nfs_fsinfo *info)
  3075. {
  3076. struct nfs4_exception exception = { };
  3077. int err;
  3078. do {
  3079. err = _nfs4_lookup_root(server, fhandle, info);
  3080. trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
  3081. switch (err) {
  3082. case 0:
  3083. case -NFS4ERR_WRONGSEC:
  3084. goto out;
  3085. default:
  3086. err = nfs4_handle_exception(server, err, &exception);
  3087. }
  3088. } while (exception.retry);
  3089. out:
  3090. return err;
  3091. }
  3092. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3093. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  3094. {
  3095. struct rpc_auth_create_args auth_args = {
  3096. .pseudoflavor = flavor,
  3097. };
  3098. struct rpc_auth *auth;
  3099. auth = rpcauth_create(&auth_args, server->client);
  3100. if (IS_ERR(auth))
  3101. return -EACCES;
  3102. return nfs4_lookup_root(server, fhandle, info);
  3103. }
  3104. /*
  3105. * Retry pseudoroot lookup with various security flavors. We do this when:
  3106. *
  3107. * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
  3108. * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
  3109. *
  3110. * Returns zero on success, or a negative NFS4ERR value, or a
  3111. * negative errno value.
  3112. */
  3113. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3114. struct nfs_fsinfo *info)
  3115. {
  3116. /* Per 3530bis 15.33.5 */
  3117. static const rpc_authflavor_t flav_array[] = {
  3118. RPC_AUTH_GSS_KRB5P,
  3119. RPC_AUTH_GSS_KRB5I,
  3120. RPC_AUTH_GSS_KRB5,
  3121. RPC_AUTH_UNIX, /* courtesy */
  3122. RPC_AUTH_NULL,
  3123. };
  3124. int status = -EPERM;
  3125. size_t i;
  3126. if (server->auth_info.flavor_len > 0) {
  3127. /* try each flavor specified by user */
  3128. for (i = 0; i < server->auth_info.flavor_len; i++) {
  3129. status = nfs4_lookup_root_sec(server, fhandle, info,
  3130. server->auth_info.flavors[i]);
  3131. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3132. continue;
  3133. break;
  3134. }
  3135. } else {
  3136. /* no flavors specified by user, try default list */
  3137. for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
  3138. status = nfs4_lookup_root_sec(server, fhandle, info,
  3139. flav_array[i]);
  3140. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3141. continue;
  3142. break;
  3143. }
  3144. }
  3145. /*
  3146. * -EACCESS could mean that the user doesn't have correct permissions
  3147. * to access the mount. It could also mean that we tried to mount
  3148. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  3149. * existing mount programs don't handle -EACCES very well so it should
  3150. * be mapped to -EPERM instead.
  3151. */
  3152. if (status == -EACCES)
  3153. status = -EPERM;
  3154. return status;
  3155. }
  3156. /**
  3157. * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
  3158. * @server: initialized nfs_server handle
  3159. * @fhandle: we fill in the pseudo-fs root file handle
  3160. * @info: we fill in an FSINFO struct
  3161. * @auth_probe: probe the auth flavours
  3162. *
  3163. * Returns zero on success, or a negative errno.
  3164. */
  3165. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  3166. struct nfs_fsinfo *info,
  3167. bool auth_probe)
  3168. {
  3169. int status = 0;
  3170. if (!auth_probe)
  3171. status = nfs4_lookup_root(server, fhandle, info);
  3172. if (auth_probe || status == NFS4ERR_WRONGSEC)
  3173. status = server->nfs_client->cl_mvops->find_root_sec(server,
  3174. fhandle, info);
  3175. if (status == 0)
  3176. status = nfs4_server_capabilities(server, fhandle);
  3177. if (status == 0)
  3178. status = nfs4_do_fsinfo(server, fhandle, info);
  3179. return nfs4_map_errors(status);
  3180. }
  3181. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  3182. struct nfs_fsinfo *info)
  3183. {
  3184. int error;
  3185. struct nfs_fattr *fattr = info->fattr;
  3186. struct nfs4_label *label = NULL;
  3187. error = nfs4_server_capabilities(server, mntfh);
  3188. if (error < 0) {
  3189. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  3190. return error;
  3191. }
  3192. label = nfs4_label_alloc(server, GFP_KERNEL);
  3193. if (IS_ERR(label))
  3194. return PTR_ERR(label);
  3195. error = nfs4_proc_getattr(server, mntfh, fattr, label);
  3196. if (error < 0) {
  3197. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  3198. goto err_free_label;
  3199. }
  3200. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  3201. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  3202. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  3203. err_free_label:
  3204. nfs4_label_free(label);
  3205. return error;
  3206. }
  3207. /*
  3208. * Get locations and (maybe) other attributes of a referral.
  3209. * Note that we'll actually follow the referral later when
  3210. * we detect fsid mismatch in inode revalidation
  3211. */
  3212. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  3213. const struct qstr *name, struct nfs_fattr *fattr,
  3214. struct nfs_fh *fhandle)
  3215. {
  3216. int status = -ENOMEM;
  3217. struct page *page = NULL;
  3218. struct nfs4_fs_locations *locations = NULL;
  3219. page = alloc_page(GFP_KERNEL);
  3220. if (page == NULL)
  3221. goto out;
  3222. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  3223. if (locations == NULL)
  3224. goto out;
  3225. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  3226. if (status != 0)
  3227. goto out;
  3228. /*
  3229. * If the fsid didn't change, this is a migration event, not a
  3230. * referral. Cause us to drop into the exception handler, which
  3231. * will kick off migration recovery.
  3232. */
  3233. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  3234. dprintk("%s: server did not return a different fsid for"
  3235. " a referral at %s\n", __func__, name->name);
  3236. status = -NFS4ERR_MOVED;
  3237. goto out;
  3238. }
  3239. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  3240. nfs_fixup_referral_attributes(&locations->fattr);
  3241. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  3242. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  3243. memset(fhandle, 0, sizeof(struct nfs_fh));
  3244. out:
  3245. if (page)
  3246. __free_page(page);
  3247. kfree(locations);
  3248. return status;
  3249. }
  3250. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3251. struct nfs_fattr *fattr, struct nfs4_label *label)
  3252. {
  3253. struct nfs4_getattr_arg args = {
  3254. .fh = fhandle,
  3255. .bitmask = server->attr_bitmask,
  3256. };
  3257. struct nfs4_getattr_res res = {
  3258. .fattr = fattr,
  3259. .label = label,
  3260. .server = server,
  3261. };
  3262. struct rpc_message msg = {
  3263. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  3264. .rpc_argp = &args,
  3265. .rpc_resp = &res,
  3266. };
  3267. args.bitmask = nfs4_bitmask(server, label);
  3268. nfs_fattr_init(fattr);
  3269. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3270. }
  3271. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3272. struct nfs_fattr *fattr, struct nfs4_label *label)
  3273. {
  3274. struct nfs4_exception exception = { };
  3275. int err;
  3276. do {
  3277. err = _nfs4_proc_getattr(server, fhandle, fattr, label);
  3278. trace_nfs4_getattr(server, fhandle, fattr, err);
  3279. err = nfs4_handle_exception(server, err,
  3280. &exception);
  3281. } while (exception.retry);
  3282. return err;
  3283. }
  3284. /*
  3285. * The file is not closed if it is opened due to the a request to change
  3286. * the size of the file. The open call will not be needed once the
  3287. * VFS layer lookup-intents are implemented.
  3288. *
  3289. * Close is called when the inode is destroyed.
  3290. * If we haven't opened the file for O_WRONLY, we
  3291. * need to in the size_change case to obtain a stateid.
  3292. *
  3293. * Got race?
  3294. * Because OPEN is always done by name in nfsv4, it is
  3295. * possible that we opened a different file by the same
  3296. * name. We can recognize this race condition, but we
  3297. * can't do anything about it besides returning an error.
  3298. *
  3299. * This will be fixed with VFS changes (lookup-intent).
  3300. */
  3301. static int
  3302. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  3303. struct iattr *sattr)
  3304. {
  3305. struct inode *inode = d_inode(dentry);
  3306. struct rpc_cred *cred = NULL;
  3307. struct nfs_open_context *ctx = NULL;
  3308. struct nfs4_label *label = NULL;
  3309. int status;
  3310. if (pnfs_ld_layoutret_on_setattr(inode) &&
  3311. sattr->ia_valid & ATTR_SIZE &&
  3312. sattr->ia_size < i_size_read(inode))
  3313. pnfs_commit_and_return_layout(inode);
  3314. nfs_fattr_init(fattr);
  3315. /* Deal with open(O_TRUNC) */
  3316. if (sattr->ia_valid & ATTR_OPEN)
  3317. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
  3318. /* Optimization: if the end result is no change, don't RPC */
  3319. if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  3320. return 0;
  3321. /* Search for an existing open(O_WRITE) file */
  3322. if (sattr->ia_valid & ATTR_FILE) {
  3323. ctx = nfs_file_open_context(sattr->ia_file);
  3324. if (ctx)
  3325. cred = ctx->cred;
  3326. }
  3327. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  3328. if (IS_ERR(label))
  3329. return PTR_ERR(label);
  3330. status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
  3331. if (status == 0) {
  3332. nfs_setattr_update_inode(inode, sattr, fattr);
  3333. nfs_setsecurity(inode, fattr, label);
  3334. }
  3335. nfs4_label_free(label);
  3336. return status;
  3337. }
  3338. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  3339. const struct qstr *name, struct nfs_fh *fhandle,
  3340. struct nfs_fattr *fattr, struct nfs4_label *label)
  3341. {
  3342. struct nfs_server *server = NFS_SERVER(dir);
  3343. int status;
  3344. struct nfs4_lookup_arg args = {
  3345. .bitmask = server->attr_bitmask,
  3346. .dir_fh = NFS_FH(dir),
  3347. .name = name,
  3348. };
  3349. struct nfs4_lookup_res res = {
  3350. .server = server,
  3351. .fattr = fattr,
  3352. .label = label,
  3353. .fh = fhandle,
  3354. };
  3355. struct rpc_message msg = {
  3356. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  3357. .rpc_argp = &args,
  3358. .rpc_resp = &res,
  3359. };
  3360. args.bitmask = nfs4_bitmask(server, label);
  3361. nfs_fattr_init(fattr);
  3362. dprintk("NFS call lookup %s\n", name->name);
  3363. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  3364. dprintk("NFS reply lookup: %d\n", status);
  3365. return status;
  3366. }
  3367. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  3368. {
  3369. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3370. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  3371. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  3372. fattr->nlink = 2;
  3373. }
  3374. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  3375. const struct qstr *name, struct nfs_fh *fhandle,
  3376. struct nfs_fattr *fattr, struct nfs4_label *label)
  3377. {
  3378. struct nfs4_exception exception = { };
  3379. struct rpc_clnt *client = *clnt;
  3380. int err;
  3381. do {
  3382. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
  3383. trace_nfs4_lookup(dir, name, err);
  3384. switch (err) {
  3385. case -NFS4ERR_BADNAME:
  3386. err = -ENOENT;
  3387. goto out;
  3388. case -NFS4ERR_MOVED:
  3389. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  3390. if (err == -NFS4ERR_MOVED)
  3391. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  3392. goto out;
  3393. case -NFS4ERR_WRONGSEC:
  3394. err = -EPERM;
  3395. if (client != *clnt)
  3396. goto out;
  3397. client = nfs4_negotiate_security(client, dir, name);
  3398. if (IS_ERR(client))
  3399. return PTR_ERR(client);
  3400. exception.retry = 1;
  3401. break;
  3402. default:
  3403. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  3404. }
  3405. } while (exception.retry);
  3406. out:
  3407. if (err == 0)
  3408. *clnt = client;
  3409. else if (client != *clnt)
  3410. rpc_shutdown_client(client);
  3411. return err;
  3412. }
  3413. static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  3414. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  3415. struct nfs4_label *label)
  3416. {
  3417. int status;
  3418. struct rpc_clnt *client = NFS_CLIENT(dir);
  3419. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
  3420. if (client != NFS_CLIENT(dir)) {
  3421. rpc_shutdown_client(client);
  3422. nfs_fixup_secinfo_attributes(fattr);
  3423. }
  3424. return status;
  3425. }
  3426. struct rpc_clnt *
  3427. nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
  3428. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  3429. {
  3430. struct rpc_clnt *client = NFS_CLIENT(dir);
  3431. int status;
  3432. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
  3433. if (status < 0)
  3434. return ERR_PTR(status);
  3435. return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
  3436. }
  3437. static int _nfs4_proc_lookupp(struct inode *inode,
  3438. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  3439. struct nfs4_label *label)
  3440. {
  3441. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  3442. struct nfs_server *server = NFS_SERVER(inode);
  3443. int status;
  3444. struct nfs4_lookupp_arg args = {
  3445. .bitmask = server->attr_bitmask,
  3446. .fh = NFS_FH(inode),
  3447. };
  3448. struct nfs4_lookupp_res res = {
  3449. .server = server,
  3450. .fattr = fattr,
  3451. .label = label,
  3452. .fh = fhandle,
  3453. };
  3454. struct rpc_message msg = {
  3455. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
  3456. .rpc_argp = &args,
  3457. .rpc_resp = &res,
  3458. };
  3459. args.bitmask = nfs4_bitmask(server, label);
  3460. nfs_fattr_init(fattr);
  3461. dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
  3462. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
  3463. &res.seq_res, 0);
  3464. dprintk("NFS reply lookupp: %d\n", status);
  3465. return status;
  3466. }
  3467. static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
  3468. struct nfs_fattr *fattr, struct nfs4_label *label)
  3469. {
  3470. struct nfs4_exception exception = { };
  3471. int err;
  3472. do {
  3473. err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
  3474. trace_nfs4_lookupp(inode, err);
  3475. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3476. &exception);
  3477. } while (exception.retry);
  3478. return err;
  3479. }
  3480. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  3481. {
  3482. struct nfs_server *server = NFS_SERVER(inode);
  3483. struct nfs4_accessargs args = {
  3484. .fh = NFS_FH(inode),
  3485. .bitmask = server->cache_consistency_bitmask,
  3486. };
  3487. struct nfs4_accessres res = {
  3488. .server = server,
  3489. };
  3490. struct rpc_message msg = {
  3491. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  3492. .rpc_argp = &args,
  3493. .rpc_resp = &res,
  3494. .rpc_cred = entry->cred,
  3495. };
  3496. int mode = entry->mask;
  3497. int status = 0;
  3498. /*
  3499. * Determine which access bits we want to ask for...
  3500. */
  3501. if (mode & MAY_READ)
  3502. args.access |= NFS4_ACCESS_READ;
  3503. if (S_ISDIR(inode->i_mode)) {
  3504. if (mode & MAY_WRITE)
  3505. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  3506. if (mode & MAY_EXEC)
  3507. args.access |= NFS4_ACCESS_LOOKUP;
  3508. } else {
  3509. if (mode & MAY_WRITE)
  3510. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  3511. if (mode & MAY_EXEC)
  3512. args.access |= NFS4_ACCESS_EXECUTE;
  3513. }
  3514. res.fattr = nfs_alloc_fattr();
  3515. if (res.fattr == NULL)
  3516. return -ENOMEM;
  3517. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3518. if (!status) {
  3519. nfs_access_set_mask(entry, res.access);
  3520. nfs_refresh_inode(inode, res.fattr);
  3521. }
  3522. nfs_free_fattr(res.fattr);
  3523. return status;
  3524. }
  3525. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  3526. {
  3527. struct nfs4_exception exception = { };
  3528. int err;
  3529. do {
  3530. err = _nfs4_proc_access(inode, entry);
  3531. trace_nfs4_access(inode, err);
  3532. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3533. &exception);
  3534. } while (exception.retry);
  3535. return err;
  3536. }
  3537. /*
  3538. * TODO: For the time being, we don't try to get any attributes
  3539. * along with any of the zero-copy operations READ, READDIR,
  3540. * READLINK, WRITE.
  3541. *
  3542. * In the case of the first three, we want to put the GETATTR
  3543. * after the read-type operation -- this is because it is hard
  3544. * to predict the length of a GETATTR response in v4, and thus
  3545. * align the READ data correctly. This means that the GETATTR
  3546. * may end up partially falling into the page cache, and we should
  3547. * shift it into the 'tail' of the xdr_buf before processing.
  3548. * To do this efficiently, we need to know the total length
  3549. * of data received, which doesn't seem to be available outside
  3550. * of the RPC layer.
  3551. *
  3552. * In the case of WRITE, we also want to put the GETATTR after
  3553. * the operation -- in this case because we want to make sure
  3554. * we get the post-operation mtime and size.
  3555. *
  3556. * Both of these changes to the XDR layer would in fact be quite
  3557. * minor, but I decided to leave them for a subsequent patch.
  3558. */
  3559. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  3560. unsigned int pgbase, unsigned int pglen)
  3561. {
  3562. struct nfs4_readlink args = {
  3563. .fh = NFS_FH(inode),
  3564. .pgbase = pgbase,
  3565. .pglen = pglen,
  3566. .pages = &page,
  3567. };
  3568. struct nfs4_readlink_res res;
  3569. struct rpc_message msg = {
  3570. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  3571. .rpc_argp = &args,
  3572. .rpc_resp = &res,
  3573. };
  3574. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  3575. }
  3576. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  3577. unsigned int pgbase, unsigned int pglen)
  3578. {
  3579. struct nfs4_exception exception = { };
  3580. int err;
  3581. do {
  3582. err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
  3583. trace_nfs4_readlink(inode, err);
  3584. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3585. &exception);
  3586. } while (exception.retry);
  3587. return err;
  3588. }
  3589. /*
  3590. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  3591. */
  3592. static int
  3593. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  3594. int flags)
  3595. {
  3596. struct nfs_server *server = NFS_SERVER(dir);
  3597. struct nfs4_label l, *ilabel = NULL;
  3598. struct nfs_open_context *ctx;
  3599. struct nfs4_state *state;
  3600. int status = 0;
  3601. ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
  3602. if (IS_ERR(ctx))
  3603. return PTR_ERR(ctx);
  3604. ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
  3605. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  3606. sattr->ia_mode &= ~current_umask();
  3607. state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
  3608. if (IS_ERR(state)) {
  3609. status = PTR_ERR(state);
  3610. goto out;
  3611. }
  3612. out:
  3613. nfs4_label_release_security(ilabel);
  3614. put_nfs_open_context(ctx);
  3615. return status;
  3616. }
  3617. static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
  3618. {
  3619. struct nfs_server *server = NFS_SERVER(dir);
  3620. struct nfs_removeargs args = {
  3621. .fh = NFS_FH(dir),
  3622. .name = *name,
  3623. };
  3624. struct nfs_removeres res = {
  3625. .server = server,
  3626. };
  3627. struct rpc_message msg = {
  3628. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  3629. .rpc_argp = &args,
  3630. .rpc_resp = &res,
  3631. };
  3632. unsigned long timestamp = jiffies;
  3633. int status;
  3634. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  3635. if (status == 0)
  3636. update_changeattr(dir, &res.cinfo, timestamp);
  3637. return status;
  3638. }
  3639. static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
  3640. {
  3641. struct nfs4_exception exception = { };
  3642. int err;
  3643. do {
  3644. err = _nfs4_proc_remove(dir, name);
  3645. trace_nfs4_remove(dir, name, err);
  3646. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3647. &exception);
  3648. } while (exception.retry);
  3649. return err;
  3650. }
  3651. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  3652. {
  3653. struct nfs_server *server = NFS_SERVER(dir);
  3654. struct nfs_removeargs *args = msg->rpc_argp;
  3655. struct nfs_removeres *res = msg->rpc_resp;
  3656. res->server = server;
  3657. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  3658. nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
  3659. nfs_fattr_init(res->dir_attr);
  3660. }
  3661. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  3662. {
  3663. nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
  3664. &data->args.seq_args,
  3665. &data->res.seq_res,
  3666. task);
  3667. }
  3668. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  3669. {
  3670. struct nfs_unlinkdata *data = task->tk_calldata;
  3671. struct nfs_removeres *res = &data->res;
  3672. if (!nfs4_sequence_done(task, &res->seq_res))
  3673. return 0;
  3674. if (nfs4_async_handle_error(task, res->server, NULL,
  3675. &data->timeout) == -EAGAIN)
  3676. return 0;
  3677. if (task->tk_status == 0)
  3678. update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
  3679. return 1;
  3680. }
  3681. static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  3682. {
  3683. struct nfs_server *server = NFS_SERVER(dir);
  3684. struct nfs_renameargs *arg = msg->rpc_argp;
  3685. struct nfs_renameres *res = msg->rpc_resp;
  3686. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  3687. res->server = server;
  3688. nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
  3689. }
  3690. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  3691. {
  3692. nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
  3693. &data->args.seq_args,
  3694. &data->res.seq_res,
  3695. task);
  3696. }
  3697. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  3698. struct inode *new_dir)
  3699. {
  3700. struct nfs_renamedata *data = task->tk_calldata;
  3701. struct nfs_renameres *res = &data->res;
  3702. if (!nfs4_sequence_done(task, &res->seq_res))
  3703. return 0;
  3704. if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
  3705. return 0;
  3706. if (task->tk_status == 0) {
  3707. update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
  3708. if (new_dir != old_dir)
  3709. update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
  3710. }
  3711. return 1;
  3712. }
  3713. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  3714. {
  3715. struct nfs_server *server = NFS_SERVER(inode);
  3716. struct nfs4_link_arg arg = {
  3717. .fh = NFS_FH(inode),
  3718. .dir_fh = NFS_FH(dir),
  3719. .name = name,
  3720. .bitmask = server->attr_bitmask,
  3721. };
  3722. struct nfs4_link_res res = {
  3723. .server = server,
  3724. .label = NULL,
  3725. };
  3726. struct rpc_message msg = {
  3727. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  3728. .rpc_argp = &arg,
  3729. .rpc_resp = &res,
  3730. };
  3731. int status = -ENOMEM;
  3732. res.fattr = nfs_alloc_fattr();
  3733. if (res.fattr == NULL)
  3734. goto out;
  3735. res.label = nfs4_label_alloc(server, GFP_KERNEL);
  3736. if (IS_ERR(res.label)) {
  3737. status = PTR_ERR(res.label);
  3738. goto out;
  3739. }
  3740. arg.bitmask = nfs4_bitmask(server, res.label);
  3741. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3742. if (!status) {
  3743. update_changeattr(dir, &res.cinfo, res.fattr->time_start);
  3744. status = nfs_post_op_update_inode(inode, res.fattr);
  3745. if (!status)
  3746. nfs_setsecurity(inode, res.fattr, res.label);
  3747. }
  3748. nfs4_label_free(res.label);
  3749. out:
  3750. nfs_free_fattr(res.fattr);
  3751. return status;
  3752. }
  3753. static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  3754. {
  3755. struct nfs4_exception exception = { };
  3756. int err;
  3757. do {
  3758. err = nfs4_handle_exception(NFS_SERVER(inode),
  3759. _nfs4_proc_link(inode, dir, name),
  3760. &exception);
  3761. } while (exception.retry);
  3762. return err;
  3763. }
  3764. struct nfs4_createdata {
  3765. struct rpc_message msg;
  3766. struct nfs4_create_arg arg;
  3767. struct nfs4_create_res res;
  3768. struct nfs_fh fh;
  3769. struct nfs_fattr fattr;
  3770. struct nfs4_label *label;
  3771. };
  3772. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  3773. const struct qstr *name, struct iattr *sattr, u32 ftype)
  3774. {
  3775. struct nfs4_createdata *data;
  3776. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3777. if (data != NULL) {
  3778. struct nfs_server *server = NFS_SERVER(dir);
  3779. data->label = nfs4_label_alloc(server, GFP_KERNEL);
  3780. if (IS_ERR(data->label))
  3781. goto out_free;
  3782. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  3783. data->msg.rpc_argp = &data->arg;
  3784. data->msg.rpc_resp = &data->res;
  3785. data->arg.dir_fh = NFS_FH(dir);
  3786. data->arg.server = server;
  3787. data->arg.name = name;
  3788. data->arg.attrs = sattr;
  3789. data->arg.ftype = ftype;
  3790. data->arg.bitmask = nfs4_bitmask(server, data->label);
  3791. data->arg.umask = current_umask();
  3792. data->res.server = server;
  3793. data->res.fh = &data->fh;
  3794. data->res.fattr = &data->fattr;
  3795. data->res.label = data->label;
  3796. nfs_fattr_init(data->res.fattr);
  3797. }
  3798. return data;
  3799. out_free:
  3800. kfree(data);
  3801. return NULL;
  3802. }
  3803. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  3804. {
  3805. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  3806. &data->arg.seq_args, &data->res.seq_res, 1);
  3807. if (status == 0) {
  3808. update_changeattr(dir, &data->res.dir_cinfo,
  3809. data->res.fattr->time_start);
  3810. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
  3811. }
  3812. return status;
  3813. }
  3814. static void nfs4_free_createdata(struct nfs4_createdata *data)
  3815. {
  3816. nfs4_label_free(data->label);
  3817. kfree(data);
  3818. }
  3819. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  3820. struct page *page, unsigned int len, struct iattr *sattr,
  3821. struct nfs4_label *label)
  3822. {
  3823. struct nfs4_createdata *data;
  3824. int status = -ENAMETOOLONG;
  3825. if (len > NFS4_MAXPATHLEN)
  3826. goto out;
  3827. status = -ENOMEM;
  3828. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  3829. if (data == NULL)
  3830. goto out;
  3831. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  3832. data->arg.u.symlink.pages = &page;
  3833. data->arg.u.symlink.len = len;
  3834. data->arg.label = label;
  3835. status = nfs4_do_create(dir, dentry, data);
  3836. nfs4_free_createdata(data);
  3837. out:
  3838. return status;
  3839. }
  3840. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  3841. struct page *page, unsigned int len, struct iattr *sattr)
  3842. {
  3843. struct nfs4_exception exception = { };
  3844. struct nfs4_label l, *label = NULL;
  3845. int err;
  3846. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  3847. do {
  3848. err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
  3849. trace_nfs4_symlink(dir, &dentry->d_name, err);
  3850. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3851. &exception);
  3852. } while (exception.retry);
  3853. nfs4_label_release_security(label);
  3854. return err;
  3855. }
  3856. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  3857. struct iattr *sattr, struct nfs4_label *label)
  3858. {
  3859. struct nfs4_createdata *data;
  3860. int status = -ENOMEM;
  3861. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  3862. if (data == NULL)
  3863. goto out;
  3864. data->arg.label = label;
  3865. status = nfs4_do_create(dir, dentry, data);
  3866. nfs4_free_createdata(data);
  3867. out:
  3868. return status;
  3869. }
  3870. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  3871. struct iattr *sattr)
  3872. {
  3873. struct nfs_server *server = NFS_SERVER(dir);
  3874. struct nfs4_exception exception = { };
  3875. struct nfs4_label l, *label = NULL;
  3876. int err;
  3877. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  3878. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  3879. sattr->ia_mode &= ~current_umask();
  3880. do {
  3881. err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
  3882. trace_nfs4_mkdir(dir, &dentry->d_name, err);
  3883. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3884. &exception);
  3885. } while (exception.retry);
  3886. nfs4_label_release_security(label);
  3887. return err;
  3888. }
  3889. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  3890. u64 cookie, struct page **pages, unsigned int count, bool plus)
  3891. {
  3892. struct inode *dir = d_inode(dentry);
  3893. struct nfs4_readdir_arg args = {
  3894. .fh = NFS_FH(dir),
  3895. .pages = pages,
  3896. .pgbase = 0,
  3897. .count = count,
  3898. .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
  3899. .plus = plus,
  3900. };
  3901. struct nfs4_readdir_res res;
  3902. struct rpc_message msg = {
  3903. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  3904. .rpc_argp = &args,
  3905. .rpc_resp = &res,
  3906. .rpc_cred = cred,
  3907. };
  3908. int status;
  3909. dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
  3910. dentry,
  3911. (unsigned long long)cookie);
  3912. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  3913. res.pgbase = args.pgbase;
  3914. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  3915. if (status >= 0) {
  3916. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  3917. status += args.pgbase;
  3918. }
  3919. nfs_invalidate_atime(dir);
  3920. dprintk("%s: returns %d\n", __func__, status);
  3921. return status;
  3922. }
  3923. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  3924. u64 cookie, struct page **pages, unsigned int count, bool plus)
  3925. {
  3926. struct nfs4_exception exception = { };
  3927. int err;
  3928. do {
  3929. err = _nfs4_proc_readdir(dentry, cred, cookie,
  3930. pages, count, plus);
  3931. trace_nfs4_readdir(d_inode(dentry), err);
  3932. err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
  3933. &exception);
  3934. } while (exception.retry);
  3935. return err;
  3936. }
  3937. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3938. struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
  3939. {
  3940. struct nfs4_createdata *data;
  3941. int mode = sattr->ia_mode;
  3942. int status = -ENOMEM;
  3943. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  3944. if (data == NULL)
  3945. goto out;
  3946. if (S_ISFIFO(mode))
  3947. data->arg.ftype = NF4FIFO;
  3948. else if (S_ISBLK(mode)) {
  3949. data->arg.ftype = NF4BLK;
  3950. data->arg.u.device.specdata1 = MAJOR(rdev);
  3951. data->arg.u.device.specdata2 = MINOR(rdev);
  3952. }
  3953. else if (S_ISCHR(mode)) {
  3954. data->arg.ftype = NF4CHR;
  3955. data->arg.u.device.specdata1 = MAJOR(rdev);
  3956. data->arg.u.device.specdata2 = MINOR(rdev);
  3957. } else if (!S_ISSOCK(mode)) {
  3958. status = -EINVAL;
  3959. goto out_free;
  3960. }
  3961. data->arg.label = label;
  3962. status = nfs4_do_create(dir, dentry, data);
  3963. out_free:
  3964. nfs4_free_createdata(data);
  3965. out:
  3966. return status;
  3967. }
  3968. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3969. struct iattr *sattr, dev_t rdev)
  3970. {
  3971. struct nfs_server *server = NFS_SERVER(dir);
  3972. struct nfs4_exception exception = { };
  3973. struct nfs4_label l, *label = NULL;
  3974. int err;
  3975. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  3976. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  3977. sattr->ia_mode &= ~current_umask();
  3978. do {
  3979. err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
  3980. trace_nfs4_mknod(dir, &dentry->d_name, err);
  3981. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3982. &exception);
  3983. } while (exception.retry);
  3984. nfs4_label_release_security(label);
  3985. return err;
  3986. }
  3987. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  3988. struct nfs_fsstat *fsstat)
  3989. {
  3990. struct nfs4_statfs_arg args = {
  3991. .fh = fhandle,
  3992. .bitmask = server->attr_bitmask,
  3993. };
  3994. struct nfs4_statfs_res res = {
  3995. .fsstat = fsstat,
  3996. };
  3997. struct rpc_message msg = {
  3998. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  3999. .rpc_argp = &args,
  4000. .rpc_resp = &res,
  4001. };
  4002. nfs_fattr_init(fsstat->fattr);
  4003. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4004. }
  4005. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  4006. {
  4007. struct nfs4_exception exception = { };
  4008. int err;
  4009. do {
  4010. err = nfs4_handle_exception(server,
  4011. _nfs4_proc_statfs(server, fhandle, fsstat),
  4012. &exception);
  4013. } while (exception.retry);
  4014. return err;
  4015. }
  4016. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  4017. struct nfs_fsinfo *fsinfo)
  4018. {
  4019. struct nfs4_fsinfo_arg args = {
  4020. .fh = fhandle,
  4021. .bitmask = server->attr_bitmask,
  4022. };
  4023. struct nfs4_fsinfo_res res = {
  4024. .fsinfo = fsinfo,
  4025. };
  4026. struct rpc_message msg = {
  4027. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  4028. .rpc_argp = &args,
  4029. .rpc_resp = &res,
  4030. };
  4031. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4032. }
  4033. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4034. {
  4035. struct nfs4_exception exception = { };
  4036. unsigned long now = jiffies;
  4037. int err;
  4038. do {
  4039. err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
  4040. trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
  4041. if (err == 0) {
  4042. nfs4_set_lease_period(server->nfs_client,
  4043. fsinfo->lease_time * HZ,
  4044. now);
  4045. break;
  4046. }
  4047. err = nfs4_handle_exception(server, err, &exception);
  4048. } while (exception.retry);
  4049. return err;
  4050. }
  4051. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4052. {
  4053. int error;
  4054. nfs_fattr_init(fsinfo->fattr);
  4055. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  4056. if (error == 0) {
  4057. /* block layout checks this! */
  4058. server->pnfs_blksize = fsinfo->blksize;
  4059. set_pnfs_layoutdriver(server, fhandle, fsinfo);
  4060. }
  4061. return error;
  4062. }
  4063. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4064. struct nfs_pathconf *pathconf)
  4065. {
  4066. struct nfs4_pathconf_arg args = {
  4067. .fh = fhandle,
  4068. .bitmask = server->attr_bitmask,
  4069. };
  4070. struct nfs4_pathconf_res res = {
  4071. .pathconf = pathconf,
  4072. };
  4073. struct rpc_message msg = {
  4074. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  4075. .rpc_argp = &args,
  4076. .rpc_resp = &res,
  4077. };
  4078. /* None of the pathconf attributes are mandatory to implement */
  4079. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  4080. memset(pathconf, 0, sizeof(*pathconf));
  4081. return 0;
  4082. }
  4083. nfs_fattr_init(pathconf->fattr);
  4084. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4085. }
  4086. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4087. struct nfs_pathconf *pathconf)
  4088. {
  4089. struct nfs4_exception exception = { };
  4090. int err;
  4091. do {
  4092. err = nfs4_handle_exception(server,
  4093. _nfs4_proc_pathconf(server, fhandle, pathconf),
  4094. &exception);
  4095. } while (exception.retry);
  4096. return err;
  4097. }
  4098. int nfs4_set_rw_stateid(nfs4_stateid *stateid,
  4099. const struct nfs_open_context *ctx,
  4100. const struct nfs_lock_context *l_ctx,
  4101. fmode_t fmode)
  4102. {
  4103. return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
  4104. }
  4105. EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
  4106. static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
  4107. const struct nfs_open_context *ctx,
  4108. const struct nfs_lock_context *l_ctx,
  4109. fmode_t fmode)
  4110. {
  4111. nfs4_stateid current_stateid;
  4112. /* If the current stateid represents a lost lock, then exit */
  4113. if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
  4114. return true;
  4115. return nfs4_stateid_match(stateid, &current_stateid);
  4116. }
  4117. static bool nfs4_error_stateid_expired(int err)
  4118. {
  4119. switch (err) {
  4120. case -NFS4ERR_DELEG_REVOKED:
  4121. case -NFS4ERR_ADMIN_REVOKED:
  4122. case -NFS4ERR_BAD_STATEID:
  4123. case -NFS4ERR_STALE_STATEID:
  4124. case -NFS4ERR_OLD_STATEID:
  4125. case -NFS4ERR_OPENMODE:
  4126. case -NFS4ERR_EXPIRED:
  4127. return true;
  4128. }
  4129. return false;
  4130. }
  4131. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4132. {
  4133. struct nfs_server *server = NFS_SERVER(hdr->inode);
  4134. trace_nfs4_read(hdr, task->tk_status);
  4135. if (task->tk_status < 0) {
  4136. struct nfs4_exception exception = {
  4137. .inode = hdr->inode,
  4138. .state = hdr->args.context->state,
  4139. .stateid = &hdr->args.stateid,
  4140. };
  4141. task->tk_status = nfs4_async_handle_exception(task,
  4142. server, task->tk_status, &exception);
  4143. if (exception.retry) {
  4144. rpc_restart_call_prepare(task);
  4145. return -EAGAIN;
  4146. }
  4147. }
  4148. if (task->tk_status > 0)
  4149. renew_lease(server, hdr->timestamp);
  4150. return 0;
  4151. }
  4152. static bool nfs4_read_stateid_changed(struct rpc_task *task,
  4153. struct nfs_pgio_args *args)
  4154. {
  4155. if (!nfs4_error_stateid_expired(task->tk_status) ||
  4156. nfs4_stateid_is_current(&args->stateid,
  4157. args->context,
  4158. args->lock_context,
  4159. FMODE_READ))
  4160. return false;
  4161. rpc_restart_call_prepare(task);
  4162. return true;
  4163. }
  4164. static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4165. {
  4166. dprintk("--> %s\n", __func__);
  4167. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  4168. return -EAGAIN;
  4169. if (nfs4_read_stateid_changed(task, &hdr->args))
  4170. return -EAGAIN;
  4171. if (task->tk_status > 0)
  4172. nfs_invalidate_atime(hdr->inode);
  4173. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  4174. nfs4_read_done_cb(task, hdr);
  4175. }
  4176. static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
  4177. struct rpc_message *msg)
  4178. {
  4179. hdr->timestamp = jiffies;
  4180. if (!hdr->pgio_done_cb)
  4181. hdr->pgio_done_cb = nfs4_read_done_cb;
  4182. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  4183. nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
  4184. }
  4185. static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
  4186. struct nfs_pgio_header *hdr)
  4187. {
  4188. if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
  4189. &hdr->args.seq_args,
  4190. &hdr->res.seq_res,
  4191. task))
  4192. return 0;
  4193. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  4194. hdr->args.lock_context,
  4195. hdr->rw_mode) == -EIO)
  4196. return -EIO;
  4197. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
  4198. return -EIO;
  4199. return 0;
  4200. }
  4201. static int nfs4_write_done_cb(struct rpc_task *task,
  4202. struct nfs_pgio_header *hdr)
  4203. {
  4204. struct inode *inode = hdr->inode;
  4205. trace_nfs4_write(hdr, task->tk_status);
  4206. if (task->tk_status < 0) {
  4207. struct nfs4_exception exception = {
  4208. .inode = hdr->inode,
  4209. .state = hdr->args.context->state,
  4210. .stateid = &hdr->args.stateid,
  4211. };
  4212. task->tk_status = nfs4_async_handle_exception(task,
  4213. NFS_SERVER(inode), task->tk_status,
  4214. &exception);
  4215. if (exception.retry) {
  4216. rpc_restart_call_prepare(task);
  4217. return -EAGAIN;
  4218. }
  4219. }
  4220. if (task->tk_status >= 0) {
  4221. renew_lease(NFS_SERVER(inode), hdr->timestamp);
  4222. nfs_writeback_update_inode(hdr);
  4223. }
  4224. return 0;
  4225. }
  4226. static bool nfs4_write_stateid_changed(struct rpc_task *task,
  4227. struct nfs_pgio_args *args)
  4228. {
  4229. if (!nfs4_error_stateid_expired(task->tk_status) ||
  4230. nfs4_stateid_is_current(&args->stateid,
  4231. args->context,
  4232. args->lock_context,
  4233. FMODE_WRITE))
  4234. return false;
  4235. rpc_restart_call_prepare(task);
  4236. return true;
  4237. }
  4238. static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4239. {
  4240. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  4241. return -EAGAIN;
  4242. if (nfs4_write_stateid_changed(task, &hdr->args))
  4243. return -EAGAIN;
  4244. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  4245. nfs4_write_done_cb(task, hdr);
  4246. }
  4247. static
  4248. bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
  4249. {
  4250. /* Don't request attributes for pNFS or O_DIRECT writes */
  4251. if (hdr->ds_clp != NULL || hdr->dreq != NULL)
  4252. return false;
  4253. /* Otherwise, request attributes if and only if we don't hold
  4254. * a delegation
  4255. */
  4256. return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
  4257. }
  4258. static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
  4259. struct rpc_message *msg)
  4260. {
  4261. struct nfs_server *server = NFS_SERVER(hdr->inode);
  4262. if (!nfs4_write_need_cache_consistency_data(hdr)) {
  4263. hdr->args.bitmask = NULL;
  4264. hdr->res.fattr = NULL;
  4265. } else
  4266. hdr->args.bitmask = server->cache_consistency_bitmask;
  4267. if (!hdr->pgio_done_cb)
  4268. hdr->pgio_done_cb = nfs4_write_done_cb;
  4269. hdr->res.server = server;
  4270. hdr->timestamp = jiffies;
  4271. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  4272. nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
  4273. }
  4274. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  4275. {
  4276. nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
  4277. &data->args.seq_args,
  4278. &data->res.seq_res,
  4279. task);
  4280. }
  4281. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  4282. {
  4283. struct inode *inode = data->inode;
  4284. trace_nfs4_commit(data, task->tk_status);
  4285. if (nfs4_async_handle_error(task, NFS_SERVER(inode),
  4286. NULL, NULL) == -EAGAIN) {
  4287. rpc_restart_call_prepare(task);
  4288. return -EAGAIN;
  4289. }
  4290. return 0;
  4291. }
  4292. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  4293. {
  4294. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4295. return -EAGAIN;
  4296. return data->commit_done_cb(task, data);
  4297. }
  4298. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
  4299. {
  4300. struct nfs_server *server = NFS_SERVER(data->inode);
  4301. if (data->commit_done_cb == NULL)
  4302. data->commit_done_cb = nfs4_commit_done_cb;
  4303. data->res.server = server;
  4304. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  4305. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  4306. }
  4307. struct nfs4_renewdata {
  4308. struct nfs_client *client;
  4309. unsigned long timestamp;
  4310. };
  4311. /*
  4312. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  4313. * standalone procedure for queueing an asynchronous RENEW.
  4314. */
  4315. static void nfs4_renew_release(void *calldata)
  4316. {
  4317. struct nfs4_renewdata *data = calldata;
  4318. struct nfs_client *clp = data->client;
  4319. if (atomic_read(&clp->cl_count) > 1)
  4320. nfs4_schedule_state_renewal(clp);
  4321. nfs_put_client(clp);
  4322. kfree(data);
  4323. }
  4324. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  4325. {
  4326. struct nfs4_renewdata *data = calldata;
  4327. struct nfs_client *clp = data->client;
  4328. unsigned long timestamp = data->timestamp;
  4329. trace_nfs4_renew_async(clp, task->tk_status);
  4330. switch (task->tk_status) {
  4331. case 0:
  4332. break;
  4333. case -NFS4ERR_LEASE_MOVED:
  4334. nfs4_schedule_lease_moved_recovery(clp);
  4335. break;
  4336. default:
  4337. /* Unless we're shutting down, schedule state recovery! */
  4338. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  4339. return;
  4340. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  4341. nfs4_schedule_lease_recovery(clp);
  4342. return;
  4343. }
  4344. nfs4_schedule_path_down_recovery(clp);
  4345. }
  4346. do_renew_lease(clp, timestamp);
  4347. }
  4348. static const struct rpc_call_ops nfs4_renew_ops = {
  4349. .rpc_call_done = nfs4_renew_done,
  4350. .rpc_release = nfs4_renew_release,
  4351. };
  4352. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  4353. {
  4354. struct rpc_message msg = {
  4355. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  4356. .rpc_argp = clp,
  4357. .rpc_cred = cred,
  4358. };
  4359. struct nfs4_renewdata *data;
  4360. if (renew_flags == 0)
  4361. return 0;
  4362. if (!atomic_inc_not_zero(&clp->cl_count))
  4363. return -EIO;
  4364. data = kmalloc(sizeof(*data), GFP_NOFS);
  4365. if (data == NULL) {
  4366. nfs_put_client(clp);
  4367. return -ENOMEM;
  4368. }
  4369. data->client = clp;
  4370. data->timestamp = jiffies;
  4371. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
  4372. &nfs4_renew_ops, data);
  4373. }
  4374. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  4375. {
  4376. struct rpc_message msg = {
  4377. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  4378. .rpc_argp = clp,
  4379. .rpc_cred = cred,
  4380. };
  4381. unsigned long now = jiffies;
  4382. int status;
  4383. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4384. if (status < 0)
  4385. return status;
  4386. do_renew_lease(clp, now);
  4387. return 0;
  4388. }
  4389. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  4390. {
  4391. return server->caps & NFS_CAP_ACLS;
  4392. }
  4393. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  4394. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  4395. * the stack.
  4396. */
  4397. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  4398. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  4399. struct page **pages)
  4400. {
  4401. struct page *newpage, **spages;
  4402. int rc = 0;
  4403. size_t len;
  4404. spages = pages;
  4405. do {
  4406. len = min_t(size_t, PAGE_SIZE, buflen);
  4407. newpage = alloc_page(GFP_KERNEL);
  4408. if (newpage == NULL)
  4409. goto unwind;
  4410. memcpy(page_address(newpage), buf, len);
  4411. buf += len;
  4412. buflen -= len;
  4413. *pages++ = newpage;
  4414. rc++;
  4415. } while (buflen != 0);
  4416. return rc;
  4417. unwind:
  4418. for(; rc > 0; rc--)
  4419. __free_page(spages[rc-1]);
  4420. return -ENOMEM;
  4421. }
  4422. struct nfs4_cached_acl {
  4423. int cached;
  4424. size_t len;
  4425. char data[0];
  4426. };
  4427. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  4428. {
  4429. struct nfs_inode *nfsi = NFS_I(inode);
  4430. spin_lock(&inode->i_lock);
  4431. kfree(nfsi->nfs4_acl);
  4432. nfsi->nfs4_acl = acl;
  4433. spin_unlock(&inode->i_lock);
  4434. }
  4435. static void nfs4_zap_acl_attr(struct inode *inode)
  4436. {
  4437. nfs4_set_cached_acl(inode, NULL);
  4438. }
  4439. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  4440. {
  4441. struct nfs_inode *nfsi = NFS_I(inode);
  4442. struct nfs4_cached_acl *acl;
  4443. int ret = -ENOENT;
  4444. spin_lock(&inode->i_lock);
  4445. acl = nfsi->nfs4_acl;
  4446. if (acl == NULL)
  4447. goto out;
  4448. if (buf == NULL) /* user is just asking for length */
  4449. goto out_len;
  4450. if (acl->cached == 0)
  4451. goto out;
  4452. ret = -ERANGE; /* see getxattr(2) man page */
  4453. if (acl->len > buflen)
  4454. goto out;
  4455. memcpy(buf, acl->data, acl->len);
  4456. out_len:
  4457. ret = acl->len;
  4458. out:
  4459. spin_unlock(&inode->i_lock);
  4460. return ret;
  4461. }
  4462. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  4463. {
  4464. struct nfs4_cached_acl *acl;
  4465. size_t buflen = sizeof(*acl) + acl_len;
  4466. if (buflen <= PAGE_SIZE) {
  4467. acl = kmalloc(buflen, GFP_KERNEL);
  4468. if (acl == NULL)
  4469. goto out;
  4470. acl->cached = 1;
  4471. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  4472. } else {
  4473. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  4474. if (acl == NULL)
  4475. goto out;
  4476. acl->cached = 0;
  4477. }
  4478. acl->len = acl_len;
  4479. out:
  4480. nfs4_set_cached_acl(inode, acl);
  4481. }
  4482. /*
  4483. * The getxattr API returns the required buffer length when called with a
  4484. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  4485. * the required buf. On a NULL buf, we send a page of data to the server
  4486. * guessing that the ACL request can be serviced by a page. If so, we cache
  4487. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  4488. * the cache. If not so, we throw away the page, and cache the required
  4489. * length. The next getxattr call will then produce another round trip to
  4490. * the server, this time with the input buf of the required size.
  4491. */
  4492. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  4493. {
  4494. struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
  4495. struct nfs_getaclargs args = {
  4496. .fh = NFS_FH(inode),
  4497. .acl_pages = pages,
  4498. .acl_len = buflen,
  4499. };
  4500. struct nfs_getaclres res = {
  4501. .acl_len = buflen,
  4502. };
  4503. struct rpc_message msg = {
  4504. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  4505. .rpc_argp = &args,
  4506. .rpc_resp = &res,
  4507. };
  4508. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
  4509. int ret = -ENOMEM, i;
  4510. if (npages > ARRAY_SIZE(pages))
  4511. return -ERANGE;
  4512. for (i = 0; i < npages; i++) {
  4513. pages[i] = alloc_page(GFP_KERNEL);
  4514. if (!pages[i])
  4515. goto out_free;
  4516. }
  4517. /* for decoding across pages */
  4518. res.acl_scratch = alloc_page(GFP_KERNEL);
  4519. if (!res.acl_scratch)
  4520. goto out_free;
  4521. args.acl_len = npages * PAGE_SIZE;
  4522. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  4523. __func__, buf, buflen, npages, args.acl_len);
  4524. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  4525. &msg, &args.seq_args, &res.seq_res, 0);
  4526. if (ret)
  4527. goto out_free;
  4528. /* Handle the case where the passed-in buffer is too short */
  4529. if (res.acl_flags & NFS4_ACL_TRUNC) {
  4530. /* Did the user only issue a request for the acl length? */
  4531. if (buf == NULL)
  4532. goto out_ok;
  4533. ret = -ERANGE;
  4534. goto out_free;
  4535. }
  4536. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
  4537. if (buf) {
  4538. if (res.acl_len > buflen) {
  4539. ret = -ERANGE;
  4540. goto out_free;
  4541. }
  4542. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  4543. }
  4544. out_ok:
  4545. ret = res.acl_len;
  4546. out_free:
  4547. for (i = 0; i < npages; i++)
  4548. if (pages[i])
  4549. __free_page(pages[i]);
  4550. if (res.acl_scratch)
  4551. __free_page(res.acl_scratch);
  4552. return ret;
  4553. }
  4554. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  4555. {
  4556. struct nfs4_exception exception = { };
  4557. ssize_t ret;
  4558. do {
  4559. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  4560. trace_nfs4_get_acl(inode, ret);
  4561. if (ret >= 0)
  4562. break;
  4563. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  4564. } while (exception.retry);
  4565. return ret;
  4566. }
  4567. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  4568. {
  4569. struct nfs_server *server = NFS_SERVER(inode);
  4570. int ret;
  4571. if (!nfs4_server_supports_acls(server))
  4572. return -EOPNOTSUPP;
  4573. ret = nfs_revalidate_inode(server, inode);
  4574. if (ret < 0)
  4575. return ret;
  4576. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  4577. nfs_zap_acl_cache(inode);
  4578. ret = nfs4_read_cached_acl(inode, buf, buflen);
  4579. if (ret != -ENOENT)
  4580. /* -ENOENT is returned if there is no ACL or if there is an ACL
  4581. * but no cached acl data, just the acl length */
  4582. return ret;
  4583. return nfs4_get_acl_uncached(inode, buf, buflen);
  4584. }
  4585. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  4586. {
  4587. struct nfs_server *server = NFS_SERVER(inode);
  4588. struct page *pages[NFS4ACL_MAXPAGES];
  4589. struct nfs_setaclargs arg = {
  4590. .fh = NFS_FH(inode),
  4591. .acl_pages = pages,
  4592. .acl_len = buflen,
  4593. };
  4594. struct nfs_setaclres res;
  4595. struct rpc_message msg = {
  4596. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  4597. .rpc_argp = &arg,
  4598. .rpc_resp = &res,
  4599. };
  4600. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  4601. int ret, i;
  4602. if (!nfs4_server_supports_acls(server))
  4603. return -EOPNOTSUPP;
  4604. if (npages > ARRAY_SIZE(pages))
  4605. return -ERANGE;
  4606. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
  4607. if (i < 0)
  4608. return i;
  4609. nfs4_inode_return_delegation(inode);
  4610. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4611. /*
  4612. * Free each page after tx, so the only ref left is
  4613. * held by the network stack
  4614. */
  4615. for (; i > 0; i--)
  4616. put_page(pages[i-1]);
  4617. /*
  4618. * Acl update can result in inode attribute update.
  4619. * so mark the attribute cache invalid.
  4620. */
  4621. spin_lock(&inode->i_lock);
  4622. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  4623. spin_unlock(&inode->i_lock);
  4624. nfs_access_zap_cache(inode);
  4625. nfs_zap_acl_cache(inode);
  4626. return ret;
  4627. }
  4628. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  4629. {
  4630. struct nfs4_exception exception = { };
  4631. int err;
  4632. do {
  4633. err = __nfs4_proc_set_acl(inode, buf, buflen);
  4634. trace_nfs4_set_acl(inode, err);
  4635. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4636. &exception);
  4637. } while (exception.retry);
  4638. return err;
  4639. }
  4640. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  4641. static int _nfs4_get_security_label(struct inode *inode, void *buf,
  4642. size_t buflen)
  4643. {
  4644. struct nfs_server *server = NFS_SERVER(inode);
  4645. struct nfs_fattr fattr;
  4646. struct nfs4_label label = {0, 0, buflen, buf};
  4647. u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  4648. struct nfs4_getattr_arg arg = {
  4649. .fh = NFS_FH(inode),
  4650. .bitmask = bitmask,
  4651. };
  4652. struct nfs4_getattr_res res = {
  4653. .fattr = &fattr,
  4654. .label = &label,
  4655. .server = server,
  4656. };
  4657. struct rpc_message msg = {
  4658. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  4659. .rpc_argp = &arg,
  4660. .rpc_resp = &res,
  4661. };
  4662. int ret;
  4663. nfs_fattr_init(&fattr);
  4664. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
  4665. if (ret)
  4666. return ret;
  4667. if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
  4668. return -ENOENT;
  4669. if (buflen < label.len)
  4670. return -ERANGE;
  4671. return 0;
  4672. }
  4673. static int nfs4_get_security_label(struct inode *inode, void *buf,
  4674. size_t buflen)
  4675. {
  4676. struct nfs4_exception exception = { };
  4677. int err;
  4678. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  4679. return -EOPNOTSUPP;
  4680. do {
  4681. err = _nfs4_get_security_label(inode, buf, buflen);
  4682. trace_nfs4_get_security_label(inode, err);
  4683. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4684. &exception);
  4685. } while (exception.retry);
  4686. return err;
  4687. }
  4688. static int _nfs4_do_set_security_label(struct inode *inode,
  4689. struct nfs4_label *ilabel,
  4690. struct nfs_fattr *fattr,
  4691. struct nfs4_label *olabel)
  4692. {
  4693. struct iattr sattr = {0};
  4694. struct nfs_server *server = NFS_SERVER(inode);
  4695. const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  4696. struct nfs_setattrargs arg = {
  4697. .fh = NFS_FH(inode),
  4698. .iap = &sattr,
  4699. .server = server,
  4700. .bitmask = bitmask,
  4701. .label = ilabel,
  4702. };
  4703. struct nfs_setattrres res = {
  4704. .fattr = fattr,
  4705. .label = olabel,
  4706. .server = server,
  4707. };
  4708. struct rpc_message msg = {
  4709. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  4710. .rpc_argp = &arg,
  4711. .rpc_resp = &res,
  4712. };
  4713. int status;
  4714. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  4715. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4716. if (status)
  4717. dprintk("%s failed: %d\n", __func__, status);
  4718. return status;
  4719. }
  4720. static int nfs4_do_set_security_label(struct inode *inode,
  4721. struct nfs4_label *ilabel,
  4722. struct nfs_fattr *fattr,
  4723. struct nfs4_label *olabel)
  4724. {
  4725. struct nfs4_exception exception = { };
  4726. int err;
  4727. do {
  4728. err = _nfs4_do_set_security_label(inode, ilabel,
  4729. fattr, olabel);
  4730. trace_nfs4_set_security_label(inode, err);
  4731. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4732. &exception);
  4733. } while (exception.retry);
  4734. return err;
  4735. }
  4736. static int
  4737. nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
  4738. {
  4739. struct nfs4_label ilabel, *olabel = NULL;
  4740. struct nfs_fattr fattr;
  4741. struct rpc_cred *cred;
  4742. int status;
  4743. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  4744. return -EOPNOTSUPP;
  4745. nfs_fattr_init(&fattr);
  4746. ilabel.pi = 0;
  4747. ilabel.lfs = 0;
  4748. ilabel.label = (char *)buf;
  4749. ilabel.len = buflen;
  4750. cred = rpc_lookup_cred();
  4751. if (IS_ERR(cred))
  4752. return PTR_ERR(cred);
  4753. olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  4754. if (IS_ERR(olabel)) {
  4755. status = -PTR_ERR(olabel);
  4756. goto out;
  4757. }
  4758. status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
  4759. if (status == 0)
  4760. nfs_setsecurity(inode, &fattr, olabel);
  4761. nfs4_label_free(olabel);
  4762. out:
  4763. put_rpccred(cred);
  4764. return status;
  4765. }
  4766. #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
  4767. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  4768. nfs4_verifier *bootverf)
  4769. {
  4770. __be32 verf[2];
  4771. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  4772. /* An impossible timestamp guarantees this value
  4773. * will never match a generated boot time. */
  4774. verf[0] = cpu_to_be32(U32_MAX);
  4775. verf[1] = cpu_to_be32(U32_MAX);
  4776. } else {
  4777. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  4778. u64 ns = ktime_to_ns(nn->boot_time);
  4779. verf[0] = cpu_to_be32(ns >> 32);
  4780. verf[1] = cpu_to_be32(ns);
  4781. }
  4782. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  4783. }
  4784. static int
  4785. nfs4_init_nonuniform_client_string(struct nfs_client *clp)
  4786. {
  4787. size_t len;
  4788. char *str;
  4789. if (clp->cl_owner_id != NULL)
  4790. return 0;
  4791. rcu_read_lock();
  4792. len = 14 + strlen(clp->cl_ipaddr) + 1 +
  4793. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
  4794. 1 +
  4795. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
  4796. 1;
  4797. rcu_read_unlock();
  4798. if (len > NFS4_OPAQUE_LIMIT + 1)
  4799. return -EINVAL;
  4800. /*
  4801. * Since this string is allocated at mount time, and held until the
  4802. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  4803. * about a memory-reclaim deadlock.
  4804. */
  4805. str = kmalloc(len, GFP_KERNEL);
  4806. if (!str)
  4807. return -ENOMEM;
  4808. rcu_read_lock();
  4809. scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
  4810. clp->cl_ipaddr,
  4811. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  4812. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
  4813. rcu_read_unlock();
  4814. clp->cl_owner_id = str;
  4815. return 0;
  4816. }
  4817. static int
  4818. nfs4_init_uniquifier_client_string(struct nfs_client *clp)
  4819. {
  4820. size_t len;
  4821. char *str;
  4822. len = 10 + 10 + 1 + 10 + 1 +
  4823. strlen(nfs4_client_id_uniquifier) + 1 +
  4824. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  4825. if (len > NFS4_OPAQUE_LIMIT + 1)
  4826. return -EINVAL;
  4827. /*
  4828. * Since this string is allocated at mount time, and held until the
  4829. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  4830. * about a memory-reclaim deadlock.
  4831. */
  4832. str = kmalloc(len, GFP_KERNEL);
  4833. if (!str)
  4834. return -ENOMEM;
  4835. scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
  4836. clp->rpc_ops->version, clp->cl_minorversion,
  4837. nfs4_client_id_uniquifier,
  4838. clp->cl_rpcclient->cl_nodename);
  4839. clp->cl_owner_id = str;
  4840. return 0;
  4841. }
  4842. static int
  4843. nfs4_init_uniform_client_string(struct nfs_client *clp)
  4844. {
  4845. size_t len;
  4846. char *str;
  4847. if (clp->cl_owner_id != NULL)
  4848. return 0;
  4849. if (nfs4_client_id_uniquifier[0] != '\0')
  4850. return nfs4_init_uniquifier_client_string(clp);
  4851. len = 10 + 10 + 1 + 10 + 1 +
  4852. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  4853. if (len > NFS4_OPAQUE_LIMIT + 1)
  4854. return -EINVAL;
  4855. /*
  4856. * Since this string is allocated at mount time, and held until the
  4857. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  4858. * about a memory-reclaim deadlock.
  4859. */
  4860. str = kmalloc(len, GFP_KERNEL);
  4861. if (!str)
  4862. return -ENOMEM;
  4863. scnprintf(str, len, "Linux NFSv%u.%u %s",
  4864. clp->rpc_ops->version, clp->cl_minorversion,
  4865. clp->cl_rpcclient->cl_nodename);
  4866. clp->cl_owner_id = str;
  4867. return 0;
  4868. }
  4869. /*
  4870. * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
  4871. * services. Advertise one based on the address family of the
  4872. * clientaddr.
  4873. */
  4874. static unsigned int
  4875. nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
  4876. {
  4877. if (strchr(clp->cl_ipaddr, ':') != NULL)
  4878. return scnprintf(buf, len, "tcp6");
  4879. else
  4880. return scnprintf(buf, len, "tcp");
  4881. }
  4882. static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
  4883. {
  4884. struct nfs4_setclientid *sc = calldata;
  4885. if (task->tk_status == 0)
  4886. sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
  4887. }
  4888. static const struct rpc_call_ops nfs4_setclientid_ops = {
  4889. .rpc_call_done = nfs4_setclientid_done,
  4890. };
  4891. /**
  4892. * nfs4_proc_setclientid - Negotiate client ID
  4893. * @clp: state data structure
  4894. * @program: RPC program for NFSv4 callback service
  4895. * @port: IP port number for NFS4 callback service
  4896. * @cred: RPC credential to use for this call
  4897. * @res: where to place the result
  4898. *
  4899. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4900. */
  4901. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  4902. unsigned short port, struct rpc_cred *cred,
  4903. struct nfs4_setclientid_res *res)
  4904. {
  4905. nfs4_verifier sc_verifier;
  4906. struct nfs4_setclientid setclientid = {
  4907. .sc_verifier = &sc_verifier,
  4908. .sc_prog = program,
  4909. .sc_clnt = clp,
  4910. };
  4911. struct rpc_message msg = {
  4912. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  4913. .rpc_argp = &setclientid,
  4914. .rpc_resp = res,
  4915. .rpc_cred = cred,
  4916. };
  4917. struct rpc_task *task;
  4918. struct rpc_task_setup task_setup_data = {
  4919. .rpc_client = clp->cl_rpcclient,
  4920. .rpc_message = &msg,
  4921. .callback_ops = &nfs4_setclientid_ops,
  4922. .callback_data = &setclientid,
  4923. .flags = RPC_TASK_TIMEOUT,
  4924. };
  4925. int status;
  4926. /* nfs_client_id4 */
  4927. nfs4_init_boot_verifier(clp, &sc_verifier);
  4928. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  4929. status = nfs4_init_uniform_client_string(clp);
  4930. else
  4931. status = nfs4_init_nonuniform_client_string(clp);
  4932. if (status)
  4933. goto out;
  4934. /* cb_client4 */
  4935. setclientid.sc_netid_len =
  4936. nfs4_init_callback_netid(clp,
  4937. setclientid.sc_netid,
  4938. sizeof(setclientid.sc_netid));
  4939. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  4940. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  4941. clp->cl_ipaddr, port >> 8, port & 255);
  4942. dprintk("NFS call setclientid auth=%s, '%s'\n",
  4943. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4944. clp->cl_owner_id);
  4945. task = rpc_run_task(&task_setup_data);
  4946. if (IS_ERR(task)) {
  4947. status = PTR_ERR(task);
  4948. goto out;
  4949. }
  4950. status = task->tk_status;
  4951. if (setclientid.sc_cred) {
  4952. clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
  4953. put_rpccred(setclientid.sc_cred);
  4954. }
  4955. rpc_put_task(task);
  4956. out:
  4957. trace_nfs4_setclientid(clp, status);
  4958. dprintk("NFS reply setclientid: %d\n", status);
  4959. return status;
  4960. }
  4961. /**
  4962. * nfs4_proc_setclientid_confirm - Confirm client ID
  4963. * @clp: state data structure
  4964. * @res: result of a previous SETCLIENTID
  4965. * @cred: RPC credential to use for this call
  4966. *
  4967. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4968. */
  4969. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  4970. struct nfs4_setclientid_res *arg,
  4971. struct rpc_cred *cred)
  4972. {
  4973. struct rpc_message msg = {
  4974. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  4975. .rpc_argp = arg,
  4976. .rpc_cred = cred,
  4977. };
  4978. int status;
  4979. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  4980. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4981. clp->cl_clientid);
  4982. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4983. trace_nfs4_setclientid_confirm(clp, status);
  4984. dprintk("NFS reply setclientid_confirm: %d\n", status);
  4985. return status;
  4986. }
  4987. struct nfs4_delegreturndata {
  4988. struct nfs4_delegreturnargs args;
  4989. struct nfs4_delegreturnres res;
  4990. struct nfs_fh fh;
  4991. nfs4_stateid stateid;
  4992. unsigned long timestamp;
  4993. struct {
  4994. struct nfs4_layoutreturn_args arg;
  4995. struct nfs4_layoutreturn_res res;
  4996. struct nfs4_xdr_opaque_data ld_private;
  4997. u32 roc_barrier;
  4998. bool roc;
  4999. } lr;
  5000. struct nfs_fattr fattr;
  5001. int rpc_status;
  5002. struct inode *inode;
  5003. };
  5004. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  5005. {
  5006. struct nfs4_delegreturndata *data = calldata;
  5007. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5008. return;
  5009. trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
  5010. /* Handle Layoutreturn errors */
  5011. if (data->args.lr_args && task->tk_status != 0) {
  5012. switch(data->res.lr_ret) {
  5013. default:
  5014. data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  5015. break;
  5016. case 0:
  5017. data->args.lr_args = NULL;
  5018. data->res.lr_res = NULL;
  5019. break;
  5020. case -NFS4ERR_ADMIN_REVOKED:
  5021. case -NFS4ERR_DELEG_REVOKED:
  5022. case -NFS4ERR_EXPIRED:
  5023. case -NFS4ERR_BAD_STATEID:
  5024. case -NFS4ERR_OLD_STATEID:
  5025. case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
  5026. case -NFS4ERR_WRONG_CRED:
  5027. data->args.lr_args = NULL;
  5028. data->res.lr_res = NULL;
  5029. data->res.lr_ret = 0;
  5030. rpc_restart_call_prepare(task);
  5031. return;
  5032. }
  5033. }
  5034. switch (task->tk_status) {
  5035. case 0:
  5036. renew_lease(data->res.server, data->timestamp);
  5037. break;
  5038. case -NFS4ERR_ADMIN_REVOKED:
  5039. case -NFS4ERR_DELEG_REVOKED:
  5040. case -NFS4ERR_EXPIRED:
  5041. nfs4_free_revoked_stateid(data->res.server,
  5042. data->args.stateid,
  5043. task->tk_msg.rpc_cred);
  5044. case -NFS4ERR_BAD_STATEID:
  5045. case -NFS4ERR_OLD_STATEID:
  5046. case -NFS4ERR_STALE_STATEID:
  5047. task->tk_status = 0;
  5048. break;
  5049. case -NFS4ERR_ACCESS:
  5050. if (data->args.bitmask) {
  5051. data->args.bitmask = NULL;
  5052. data->res.fattr = NULL;
  5053. task->tk_status = 0;
  5054. rpc_restart_call_prepare(task);
  5055. return;
  5056. }
  5057. default:
  5058. if (nfs4_async_handle_error(task, data->res.server,
  5059. NULL, NULL) == -EAGAIN) {
  5060. rpc_restart_call_prepare(task);
  5061. return;
  5062. }
  5063. }
  5064. data->rpc_status = task->tk_status;
  5065. }
  5066. static void nfs4_delegreturn_release(void *calldata)
  5067. {
  5068. struct nfs4_delegreturndata *data = calldata;
  5069. struct inode *inode = data->inode;
  5070. if (inode) {
  5071. if (data->lr.roc)
  5072. pnfs_roc_release(&data->lr.arg, &data->lr.res,
  5073. data->res.lr_ret);
  5074. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  5075. nfs_iput_and_deactive(inode);
  5076. }
  5077. kfree(calldata);
  5078. }
  5079. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  5080. {
  5081. struct nfs4_delegreturndata *d_data;
  5082. d_data = (struct nfs4_delegreturndata *)data;
  5083. if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
  5084. return;
  5085. nfs4_setup_sequence(d_data->res.server->nfs_client,
  5086. &d_data->args.seq_args,
  5087. &d_data->res.seq_res,
  5088. task);
  5089. }
  5090. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  5091. .rpc_call_prepare = nfs4_delegreturn_prepare,
  5092. .rpc_call_done = nfs4_delegreturn_done,
  5093. .rpc_release = nfs4_delegreturn_release,
  5094. };
  5095. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  5096. {
  5097. struct nfs4_delegreturndata *data;
  5098. struct nfs_server *server = NFS_SERVER(inode);
  5099. struct rpc_task *task;
  5100. struct rpc_message msg = {
  5101. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  5102. .rpc_cred = cred,
  5103. };
  5104. struct rpc_task_setup task_setup_data = {
  5105. .rpc_client = server->client,
  5106. .rpc_message = &msg,
  5107. .callback_ops = &nfs4_delegreturn_ops,
  5108. .flags = RPC_TASK_ASYNC,
  5109. };
  5110. int status = 0;
  5111. data = kzalloc(sizeof(*data), GFP_NOFS);
  5112. if (data == NULL)
  5113. return -ENOMEM;
  5114. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  5115. nfs4_state_protect(server->nfs_client,
  5116. NFS_SP4_MACH_CRED_CLEANUP,
  5117. &task_setup_data.rpc_client, &msg);
  5118. data->args.fhandle = &data->fh;
  5119. data->args.stateid = &data->stateid;
  5120. data->args.bitmask = server->cache_consistency_bitmask;
  5121. nfs_copy_fh(&data->fh, NFS_FH(inode));
  5122. nfs4_stateid_copy(&data->stateid, stateid);
  5123. data->res.fattr = &data->fattr;
  5124. data->res.server = server;
  5125. data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  5126. data->lr.arg.ld_private = &data->lr.ld_private;
  5127. nfs_fattr_init(data->res.fattr);
  5128. data->timestamp = jiffies;
  5129. data->rpc_status = 0;
  5130. data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
  5131. data->inode = nfs_igrab_and_active(inode);
  5132. if (data->inode) {
  5133. if (data->lr.roc) {
  5134. data->args.lr_args = &data->lr.arg;
  5135. data->res.lr_res = &data->lr.res;
  5136. }
  5137. } else if (data->lr.roc) {
  5138. pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
  5139. data->lr.roc = false;
  5140. }
  5141. task_setup_data.callback_data = data;
  5142. msg.rpc_argp = &data->args;
  5143. msg.rpc_resp = &data->res;
  5144. task = rpc_run_task(&task_setup_data);
  5145. if (IS_ERR(task))
  5146. return PTR_ERR(task);
  5147. if (!issync)
  5148. goto out;
  5149. status = rpc_wait_for_completion_task(task);
  5150. if (status != 0)
  5151. goto out;
  5152. status = data->rpc_status;
  5153. out:
  5154. rpc_put_task(task);
  5155. return status;
  5156. }
  5157. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  5158. {
  5159. struct nfs_server *server = NFS_SERVER(inode);
  5160. struct nfs4_exception exception = { };
  5161. int err;
  5162. do {
  5163. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  5164. trace_nfs4_delegreturn(inode, stateid, err);
  5165. switch (err) {
  5166. case -NFS4ERR_STALE_STATEID:
  5167. case -NFS4ERR_EXPIRED:
  5168. case 0:
  5169. return 0;
  5170. }
  5171. err = nfs4_handle_exception(server, err, &exception);
  5172. } while (exception.retry);
  5173. return err;
  5174. }
  5175. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5176. {
  5177. struct inode *inode = state->inode;
  5178. struct nfs_server *server = NFS_SERVER(inode);
  5179. struct nfs_client *clp = server->nfs_client;
  5180. struct nfs_lockt_args arg = {
  5181. .fh = NFS_FH(inode),
  5182. .fl = request,
  5183. };
  5184. struct nfs_lockt_res res = {
  5185. .denied = request,
  5186. };
  5187. struct rpc_message msg = {
  5188. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  5189. .rpc_argp = &arg,
  5190. .rpc_resp = &res,
  5191. .rpc_cred = state->owner->so_cred,
  5192. };
  5193. struct nfs4_lock_state *lsp;
  5194. int status;
  5195. arg.lock_owner.clientid = clp->cl_clientid;
  5196. status = nfs4_set_lock_state(state, request);
  5197. if (status != 0)
  5198. goto out;
  5199. lsp = request->fl_u.nfs4_fl.owner;
  5200. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  5201. arg.lock_owner.s_dev = server->s_dev;
  5202. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  5203. switch (status) {
  5204. case 0:
  5205. request->fl_type = F_UNLCK;
  5206. break;
  5207. case -NFS4ERR_DENIED:
  5208. status = 0;
  5209. }
  5210. request->fl_ops->fl_release_private(request);
  5211. request->fl_ops = NULL;
  5212. out:
  5213. return status;
  5214. }
  5215. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5216. {
  5217. struct nfs4_exception exception = { };
  5218. int err;
  5219. do {
  5220. err = _nfs4_proc_getlk(state, cmd, request);
  5221. trace_nfs4_get_lock(request, state, cmd, err);
  5222. err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
  5223. &exception);
  5224. } while (exception.retry);
  5225. return err;
  5226. }
  5227. struct nfs4_unlockdata {
  5228. struct nfs_locku_args arg;
  5229. struct nfs_locku_res res;
  5230. struct nfs4_lock_state *lsp;
  5231. struct nfs_open_context *ctx;
  5232. struct nfs_lock_context *l_ctx;
  5233. struct file_lock fl;
  5234. struct nfs_server *server;
  5235. unsigned long timestamp;
  5236. };
  5237. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  5238. struct nfs_open_context *ctx,
  5239. struct nfs4_lock_state *lsp,
  5240. struct nfs_seqid *seqid)
  5241. {
  5242. struct nfs4_unlockdata *p;
  5243. struct inode *inode = lsp->ls_state->inode;
  5244. p = kzalloc(sizeof(*p), GFP_NOFS);
  5245. if (p == NULL)
  5246. return NULL;
  5247. p->arg.fh = NFS_FH(inode);
  5248. p->arg.fl = &p->fl;
  5249. p->arg.seqid = seqid;
  5250. p->res.seqid = seqid;
  5251. p->lsp = lsp;
  5252. atomic_inc(&lsp->ls_count);
  5253. /* Ensure we don't close file until we're done freeing locks! */
  5254. p->ctx = get_nfs_open_context(ctx);
  5255. p->l_ctx = nfs_get_lock_context(ctx);
  5256. memcpy(&p->fl, fl, sizeof(p->fl));
  5257. p->server = NFS_SERVER(inode);
  5258. return p;
  5259. }
  5260. static void nfs4_locku_release_calldata(void *data)
  5261. {
  5262. struct nfs4_unlockdata *calldata = data;
  5263. nfs_free_seqid(calldata->arg.seqid);
  5264. nfs4_put_lock_state(calldata->lsp);
  5265. nfs_put_lock_context(calldata->l_ctx);
  5266. put_nfs_open_context(calldata->ctx);
  5267. kfree(calldata);
  5268. }
  5269. static void nfs4_locku_done(struct rpc_task *task, void *data)
  5270. {
  5271. struct nfs4_unlockdata *calldata = data;
  5272. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  5273. return;
  5274. switch (task->tk_status) {
  5275. case 0:
  5276. renew_lease(calldata->server, calldata->timestamp);
  5277. locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
  5278. if (nfs4_update_lock_stateid(calldata->lsp,
  5279. &calldata->res.stateid))
  5280. break;
  5281. case -NFS4ERR_ADMIN_REVOKED:
  5282. case -NFS4ERR_EXPIRED:
  5283. nfs4_free_revoked_stateid(calldata->server,
  5284. &calldata->arg.stateid,
  5285. task->tk_msg.rpc_cred);
  5286. case -NFS4ERR_BAD_STATEID:
  5287. case -NFS4ERR_OLD_STATEID:
  5288. case -NFS4ERR_STALE_STATEID:
  5289. if (!nfs4_stateid_match(&calldata->arg.stateid,
  5290. &calldata->lsp->ls_stateid))
  5291. rpc_restart_call_prepare(task);
  5292. break;
  5293. default:
  5294. if (nfs4_async_handle_error(task, calldata->server,
  5295. NULL, NULL) == -EAGAIN)
  5296. rpc_restart_call_prepare(task);
  5297. }
  5298. nfs_release_seqid(calldata->arg.seqid);
  5299. }
  5300. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  5301. {
  5302. struct nfs4_unlockdata *calldata = data;
  5303. if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
  5304. nfs_async_iocounter_wait(task, calldata->l_ctx))
  5305. return;
  5306. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  5307. goto out_wait;
  5308. nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
  5309. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  5310. /* Note: exit _without_ running nfs4_locku_done */
  5311. goto out_no_action;
  5312. }
  5313. calldata->timestamp = jiffies;
  5314. if (nfs4_setup_sequence(calldata->server->nfs_client,
  5315. &calldata->arg.seq_args,
  5316. &calldata->res.seq_res,
  5317. task) != 0)
  5318. nfs_release_seqid(calldata->arg.seqid);
  5319. return;
  5320. out_no_action:
  5321. task->tk_action = NULL;
  5322. out_wait:
  5323. nfs4_sequence_done(task, &calldata->res.seq_res);
  5324. }
  5325. static const struct rpc_call_ops nfs4_locku_ops = {
  5326. .rpc_call_prepare = nfs4_locku_prepare,
  5327. .rpc_call_done = nfs4_locku_done,
  5328. .rpc_release = nfs4_locku_release_calldata,
  5329. };
  5330. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  5331. struct nfs_open_context *ctx,
  5332. struct nfs4_lock_state *lsp,
  5333. struct nfs_seqid *seqid)
  5334. {
  5335. struct nfs4_unlockdata *data;
  5336. struct rpc_message msg = {
  5337. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  5338. .rpc_cred = ctx->cred,
  5339. };
  5340. struct rpc_task_setup task_setup_data = {
  5341. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  5342. .rpc_message = &msg,
  5343. .callback_ops = &nfs4_locku_ops,
  5344. .workqueue = nfsiod_workqueue,
  5345. .flags = RPC_TASK_ASYNC,
  5346. };
  5347. nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
  5348. NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
  5349. /* Ensure this is an unlock - when canceling a lock, the
  5350. * canceled lock is passed in, and it won't be an unlock.
  5351. */
  5352. fl->fl_type = F_UNLCK;
  5353. if (fl->fl_flags & FL_CLOSE)
  5354. set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
  5355. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  5356. if (data == NULL) {
  5357. nfs_free_seqid(seqid);
  5358. return ERR_PTR(-ENOMEM);
  5359. }
  5360. nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  5361. msg.rpc_argp = &data->arg;
  5362. msg.rpc_resp = &data->res;
  5363. task_setup_data.callback_data = data;
  5364. return rpc_run_task(&task_setup_data);
  5365. }
  5366. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  5367. {
  5368. struct inode *inode = state->inode;
  5369. struct nfs4_state_owner *sp = state->owner;
  5370. struct nfs_inode *nfsi = NFS_I(inode);
  5371. struct nfs_seqid *seqid;
  5372. struct nfs4_lock_state *lsp;
  5373. struct rpc_task *task;
  5374. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  5375. int status = 0;
  5376. unsigned char fl_flags = request->fl_flags;
  5377. status = nfs4_set_lock_state(state, request);
  5378. /* Unlock _before_ we do the RPC call */
  5379. request->fl_flags |= FL_EXISTS;
  5380. /* Exclude nfs_delegation_claim_locks() */
  5381. mutex_lock(&sp->so_delegreturn_mutex);
  5382. /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
  5383. down_read(&nfsi->rwsem);
  5384. if (locks_lock_inode_wait(inode, request) == -ENOENT) {
  5385. up_read(&nfsi->rwsem);
  5386. mutex_unlock(&sp->so_delegreturn_mutex);
  5387. goto out;
  5388. }
  5389. up_read(&nfsi->rwsem);
  5390. mutex_unlock(&sp->so_delegreturn_mutex);
  5391. if (status != 0)
  5392. goto out;
  5393. /* Is this a delegated lock? */
  5394. lsp = request->fl_u.nfs4_fl.owner;
  5395. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
  5396. goto out;
  5397. alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
  5398. seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  5399. status = -ENOMEM;
  5400. if (IS_ERR(seqid))
  5401. goto out;
  5402. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  5403. status = PTR_ERR(task);
  5404. if (IS_ERR(task))
  5405. goto out;
  5406. status = rpc_wait_for_completion_task(task);
  5407. rpc_put_task(task);
  5408. out:
  5409. request->fl_flags = fl_flags;
  5410. trace_nfs4_unlock(request, state, F_SETLK, status);
  5411. return status;
  5412. }
  5413. struct nfs4_lockdata {
  5414. struct nfs_lock_args arg;
  5415. struct nfs_lock_res res;
  5416. struct nfs4_lock_state *lsp;
  5417. struct nfs_open_context *ctx;
  5418. struct file_lock fl;
  5419. unsigned long timestamp;
  5420. int rpc_status;
  5421. int cancelled;
  5422. struct nfs_server *server;
  5423. };
  5424. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  5425. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  5426. gfp_t gfp_mask)
  5427. {
  5428. struct nfs4_lockdata *p;
  5429. struct inode *inode = lsp->ls_state->inode;
  5430. struct nfs_server *server = NFS_SERVER(inode);
  5431. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  5432. p = kzalloc(sizeof(*p), gfp_mask);
  5433. if (p == NULL)
  5434. return NULL;
  5435. p->arg.fh = NFS_FH(inode);
  5436. p->arg.fl = &p->fl;
  5437. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  5438. if (IS_ERR(p->arg.open_seqid))
  5439. goto out_free;
  5440. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  5441. p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
  5442. if (IS_ERR(p->arg.lock_seqid))
  5443. goto out_free_seqid;
  5444. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  5445. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  5446. p->arg.lock_owner.s_dev = server->s_dev;
  5447. p->res.lock_seqid = p->arg.lock_seqid;
  5448. p->lsp = lsp;
  5449. p->server = server;
  5450. atomic_inc(&lsp->ls_count);
  5451. p->ctx = get_nfs_open_context(ctx);
  5452. memcpy(&p->fl, fl, sizeof(p->fl));
  5453. return p;
  5454. out_free_seqid:
  5455. nfs_free_seqid(p->arg.open_seqid);
  5456. out_free:
  5457. kfree(p);
  5458. return NULL;
  5459. }
  5460. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  5461. {
  5462. struct nfs4_lockdata *data = calldata;
  5463. struct nfs4_state *state = data->lsp->ls_state;
  5464. dprintk("%s: begin!\n", __func__);
  5465. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  5466. goto out_wait;
  5467. /* Do we need to do an open_to_lock_owner? */
  5468. if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
  5469. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  5470. goto out_release_lock_seqid;
  5471. }
  5472. nfs4_stateid_copy(&data->arg.open_stateid,
  5473. &state->open_stateid);
  5474. data->arg.new_lock_owner = 1;
  5475. data->res.open_seqid = data->arg.open_seqid;
  5476. } else {
  5477. data->arg.new_lock_owner = 0;
  5478. nfs4_stateid_copy(&data->arg.lock_stateid,
  5479. &data->lsp->ls_stateid);
  5480. }
  5481. if (!nfs4_valid_open_stateid(state)) {
  5482. data->rpc_status = -EBADF;
  5483. task->tk_action = NULL;
  5484. goto out_release_open_seqid;
  5485. }
  5486. data->timestamp = jiffies;
  5487. if (nfs4_setup_sequence(data->server->nfs_client,
  5488. &data->arg.seq_args,
  5489. &data->res.seq_res,
  5490. task) == 0)
  5491. return;
  5492. out_release_open_seqid:
  5493. nfs_release_seqid(data->arg.open_seqid);
  5494. out_release_lock_seqid:
  5495. nfs_release_seqid(data->arg.lock_seqid);
  5496. out_wait:
  5497. nfs4_sequence_done(task, &data->res.seq_res);
  5498. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  5499. }
  5500. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  5501. {
  5502. struct nfs4_lockdata *data = calldata;
  5503. struct nfs4_lock_state *lsp = data->lsp;
  5504. dprintk("%s: begin!\n", __func__);
  5505. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5506. return;
  5507. data->rpc_status = task->tk_status;
  5508. switch (task->tk_status) {
  5509. case 0:
  5510. renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
  5511. data->timestamp);
  5512. if (data->arg.new_lock) {
  5513. data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
  5514. if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
  5515. rpc_restart_call_prepare(task);
  5516. break;
  5517. }
  5518. }
  5519. if (data->arg.new_lock_owner != 0) {
  5520. nfs_confirm_seqid(&lsp->ls_seqid, 0);
  5521. nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
  5522. set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  5523. } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
  5524. rpc_restart_call_prepare(task);
  5525. break;
  5526. case -NFS4ERR_BAD_STATEID:
  5527. case -NFS4ERR_OLD_STATEID:
  5528. case -NFS4ERR_STALE_STATEID:
  5529. case -NFS4ERR_EXPIRED:
  5530. if (data->arg.new_lock_owner != 0) {
  5531. if (!nfs4_stateid_match(&data->arg.open_stateid,
  5532. &lsp->ls_state->open_stateid))
  5533. rpc_restart_call_prepare(task);
  5534. } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
  5535. &lsp->ls_stateid))
  5536. rpc_restart_call_prepare(task);
  5537. }
  5538. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  5539. }
  5540. static void nfs4_lock_release(void *calldata)
  5541. {
  5542. struct nfs4_lockdata *data = calldata;
  5543. dprintk("%s: begin!\n", __func__);
  5544. nfs_free_seqid(data->arg.open_seqid);
  5545. if (data->cancelled) {
  5546. struct rpc_task *task;
  5547. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  5548. data->arg.lock_seqid);
  5549. if (!IS_ERR(task))
  5550. rpc_put_task_async(task);
  5551. dprintk("%s: cancelling lock!\n", __func__);
  5552. } else
  5553. nfs_free_seqid(data->arg.lock_seqid);
  5554. nfs4_put_lock_state(data->lsp);
  5555. put_nfs_open_context(data->ctx);
  5556. kfree(data);
  5557. dprintk("%s: done!\n", __func__);
  5558. }
  5559. static const struct rpc_call_ops nfs4_lock_ops = {
  5560. .rpc_call_prepare = nfs4_lock_prepare,
  5561. .rpc_call_done = nfs4_lock_done,
  5562. .rpc_release = nfs4_lock_release,
  5563. };
  5564. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  5565. {
  5566. switch (error) {
  5567. case -NFS4ERR_ADMIN_REVOKED:
  5568. case -NFS4ERR_EXPIRED:
  5569. case -NFS4ERR_BAD_STATEID:
  5570. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  5571. if (new_lock_owner != 0 ||
  5572. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  5573. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  5574. break;
  5575. case -NFS4ERR_STALE_STATEID:
  5576. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  5577. nfs4_schedule_lease_recovery(server->nfs_client);
  5578. };
  5579. }
  5580. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  5581. {
  5582. struct nfs4_lockdata *data;
  5583. struct rpc_task *task;
  5584. struct rpc_message msg = {
  5585. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  5586. .rpc_cred = state->owner->so_cred,
  5587. };
  5588. struct rpc_task_setup task_setup_data = {
  5589. .rpc_client = NFS_CLIENT(state->inode),
  5590. .rpc_message = &msg,
  5591. .callback_ops = &nfs4_lock_ops,
  5592. .workqueue = nfsiod_workqueue,
  5593. .flags = RPC_TASK_ASYNC,
  5594. };
  5595. int ret;
  5596. dprintk("%s: begin!\n", __func__);
  5597. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  5598. fl->fl_u.nfs4_fl.owner,
  5599. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  5600. if (data == NULL)
  5601. return -ENOMEM;
  5602. if (IS_SETLKW(cmd))
  5603. data->arg.block = 1;
  5604. nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  5605. msg.rpc_argp = &data->arg;
  5606. msg.rpc_resp = &data->res;
  5607. task_setup_data.callback_data = data;
  5608. if (recovery_type > NFS_LOCK_NEW) {
  5609. if (recovery_type == NFS_LOCK_RECLAIM)
  5610. data->arg.reclaim = NFS_LOCK_RECLAIM;
  5611. nfs4_set_sequence_privileged(&data->arg.seq_args);
  5612. } else
  5613. data->arg.new_lock = 1;
  5614. task = rpc_run_task(&task_setup_data);
  5615. if (IS_ERR(task))
  5616. return PTR_ERR(task);
  5617. ret = rpc_wait_for_completion_task(task);
  5618. if (ret == 0) {
  5619. ret = data->rpc_status;
  5620. if (ret)
  5621. nfs4_handle_setlk_error(data->server, data->lsp,
  5622. data->arg.new_lock_owner, ret);
  5623. } else
  5624. data->cancelled = true;
  5625. rpc_put_task(task);
  5626. dprintk("%s: done, ret = %d!\n", __func__, ret);
  5627. trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
  5628. return ret;
  5629. }
  5630. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  5631. {
  5632. struct nfs_server *server = NFS_SERVER(state->inode);
  5633. struct nfs4_exception exception = {
  5634. .inode = state->inode,
  5635. };
  5636. int err;
  5637. do {
  5638. /* Cache the lock if possible... */
  5639. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  5640. return 0;
  5641. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  5642. if (err != -NFS4ERR_DELAY)
  5643. break;
  5644. nfs4_handle_exception(server, err, &exception);
  5645. } while (exception.retry);
  5646. return err;
  5647. }
  5648. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  5649. {
  5650. struct nfs_server *server = NFS_SERVER(state->inode);
  5651. struct nfs4_exception exception = {
  5652. .inode = state->inode,
  5653. };
  5654. int err;
  5655. err = nfs4_set_lock_state(state, request);
  5656. if (err != 0)
  5657. return err;
  5658. if (!recover_lost_locks) {
  5659. set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
  5660. return 0;
  5661. }
  5662. do {
  5663. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  5664. return 0;
  5665. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  5666. switch (err) {
  5667. default:
  5668. goto out;
  5669. case -NFS4ERR_GRACE:
  5670. case -NFS4ERR_DELAY:
  5671. nfs4_handle_exception(server, err, &exception);
  5672. err = 0;
  5673. }
  5674. } while (exception.retry);
  5675. out:
  5676. return err;
  5677. }
  5678. #if defined(CONFIG_NFS_V4_1)
  5679. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  5680. {
  5681. struct nfs4_lock_state *lsp;
  5682. int status;
  5683. status = nfs4_set_lock_state(state, request);
  5684. if (status != 0)
  5685. return status;
  5686. lsp = request->fl_u.nfs4_fl.owner;
  5687. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
  5688. test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
  5689. return 0;
  5690. return nfs4_lock_expired(state, request);
  5691. }
  5692. #endif
  5693. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5694. {
  5695. struct nfs_inode *nfsi = NFS_I(state->inode);
  5696. struct nfs4_state_owner *sp = state->owner;
  5697. unsigned char fl_flags = request->fl_flags;
  5698. int status;
  5699. request->fl_flags |= FL_ACCESS;
  5700. status = locks_lock_inode_wait(state->inode, request);
  5701. if (status < 0)
  5702. goto out;
  5703. mutex_lock(&sp->so_delegreturn_mutex);
  5704. down_read(&nfsi->rwsem);
  5705. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  5706. /* Yes: cache locks! */
  5707. /* ...but avoid races with delegation recall... */
  5708. request->fl_flags = fl_flags & ~FL_SLEEP;
  5709. status = locks_lock_inode_wait(state->inode, request);
  5710. up_read(&nfsi->rwsem);
  5711. mutex_unlock(&sp->so_delegreturn_mutex);
  5712. goto out;
  5713. }
  5714. up_read(&nfsi->rwsem);
  5715. mutex_unlock(&sp->so_delegreturn_mutex);
  5716. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  5717. out:
  5718. request->fl_flags = fl_flags;
  5719. return status;
  5720. }
  5721. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5722. {
  5723. struct nfs4_exception exception = {
  5724. .state = state,
  5725. .inode = state->inode,
  5726. };
  5727. int err;
  5728. do {
  5729. err = _nfs4_proc_setlk(state, cmd, request);
  5730. if (err == -NFS4ERR_DENIED)
  5731. err = -EAGAIN;
  5732. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  5733. err, &exception);
  5734. } while (exception.retry);
  5735. return err;
  5736. }
  5737. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  5738. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  5739. static int
  5740. nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
  5741. struct file_lock *request)
  5742. {
  5743. int status = -ERESTARTSYS;
  5744. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  5745. while(!signalled()) {
  5746. status = nfs4_proc_setlk(state, cmd, request);
  5747. if ((status != -EAGAIN) || IS_SETLK(cmd))
  5748. break;
  5749. freezable_schedule_timeout_interruptible(timeout);
  5750. timeout *= 2;
  5751. timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
  5752. status = -ERESTARTSYS;
  5753. }
  5754. return status;
  5755. }
  5756. #ifdef CONFIG_NFS_V4_1
  5757. struct nfs4_lock_waiter {
  5758. struct task_struct *task;
  5759. struct inode *inode;
  5760. struct nfs_lowner *owner;
  5761. bool notified;
  5762. };
  5763. static int
  5764. nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
  5765. {
  5766. int ret;
  5767. struct cb_notify_lock_args *cbnl = key;
  5768. struct nfs4_lock_waiter *waiter = wait->private;
  5769. struct nfs_lowner *lowner = &cbnl->cbnl_owner,
  5770. *wowner = waiter->owner;
  5771. /* Only wake if the callback was for the same owner */
  5772. if (lowner->clientid != wowner->clientid ||
  5773. lowner->id != wowner->id ||
  5774. lowner->s_dev != wowner->s_dev)
  5775. return 0;
  5776. /* Make sure it's for the right inode */
  5777. if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
  5778. return 0;
  5779. waiter->notified = true;
  5780. /* override "private" so we can use default_wake_function */
  5781. wait->private = waiter->task;
  5782. ret = autoremove_wake_function(wait, mode, flags, key);
  5783. wait->private = waiter;
  5784. return ret;
  5785. }
  5786. static int
  5787. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5788. {
  5789. int status = -ERESTARTSYS;
  5790. unsigned long flags;
  5791. struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
  5792. struct nfs_server *server = NFS_SERVER(state->inode);
  5793. struct nfs_client *clp = server->nfs_client;
  5794. wait_queue_head_t *q = &clp->cl_lock_waitq;
  5795. struct nfs_lowner owner = { .clientid = clp->cl_clientid,
  5796. .id = lsp->ls_seqid.owner_id,
  5797. .s_dev = server->s_dev };
  5798. struct nfs4_lock_waiter waiter = { .task = current,
  5799. .inode = state->inode,
  5800. .owner = &owner,
  5801. .notified = false };
  5802. wait_queue_t wait;
  5803. /* Don't bother with waitqueue if we don't expect a callback */
  5804. if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
  5805. return nfs4_retry_setlk_simple(state, cmd, request);
  5806. init_wait(&wait);
  5807. wait.private = &waiter;
  5808. wait.func = nfs4_wake_lock_waiter;
  5809. add_wait_queue(q, &wait);
  5810. while(!signalled()) {
  5811. status = nfs4_proc_setlk(state, cmd, request);
  5812. if ((status != -EAGAIN) || IS_SETLK(cmd))
  5813. break;
  5814. status = -ERESTARTSYS;
  5815. spin_lock_irqsave(&q->lock, flags);
  5816. if (waiter.notified) {
  5817. spin_unlock_irqrestore(&q->lock, flags);
  5818. continue;
  5819. }
  5820. set_current_state(TASK_INTERRUPTIBLE);
  5821. spin_unlock_irqrestore(&q->lock, flags);
  5822. freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
  5823. }
  5824. finish_wait(q, &wait);
  5825. return status;
  5826. }
  5827. #else /* !CONFIG_NFS_V4_1 */
  5828. static inline int
  5829. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5830. {
  5831. return nfs4_retry_setlk_simple(state, cmd, request);
  5832. }
  5833. #endif
  5834. static int
  5835. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  5836. {
  5837. struct nfs_open_context *ctx;
  5838. struct nfs4_state *state;
  5839. int status;
  5840. /* verify open state */
  5841. ctx = nfs_file_open_context(filp);
  5842. state = ctx->state;
  5843. if (IS_GETLK(cmd)) {
  5844. if (state != NULL)
  5845. return nfs4_proc_getlk(state, F_GETLK, request);
  5846. return 0;
  5847. }
  5848. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  5849. return -EINVAL;
  5850. if (request->fl_type == F_UNLCK) {
  5851. if (state != NULL)
  5852. return nfs4_proc_unlck(state, cmd, request);
  5853. return 0;
  5854. }
  5855. if (state == NULL)
  5856. return -ENOLCK;
  5857. if ((request->fl_flags & FL_POSIX) &&
  5858. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  5859. return -ENOLCK;
  5860. status = nfs4_set_lock_state(state, request);
  5861. if (status != 0)
  5862. return status;
  5863. return nfs4_retry_setlk(state, cmd, request);
  5864. }
  5865. int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
  5866. {
  5867. struct nfs_server *server = NFS_SERVER(state->inode);
  5868. int err;
  5869. err = nfs4_set_lock_state(state, fl);
  5870. if (err != 0)
  5871. return err;
  5872. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  5873. return nfs4_handle_delegation_recall_error(server, state, stateid, err);
  5874. }
  5875. struct nfs_release_lockowner_data {
  5876. struct nfs4_lock_state *lsp;
  5877. struct nfs_server *server;
  5878. struct nfs_release_lockowner_args args;
  5879. struct nfs_release_lockowner_res res;
  5880. unsigned long timestamp;
  5881. };
  5882. static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
  5883. {
  5884. struct nfs_release_lockowner_data *data = calldata;
  5885. struct nfs_server *server = data->server;
  5886. nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
  5887. &data->res.seq_res, task);
  5888. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  5889. data->timestamp = jiffies;
  5890. }
  5891. static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
  5892. {
  5893. struct nfs_release_lockowner_data *data = calldata;
  5894. struct nfs_server *server = data->server;
  5895. nfs40_sequence_done(task, &data->res.seq_res);
  5896. switch (task->tk_status) {
  5897. case 0:
  5898. renew_lease(server, data->timestamp);
  5899. break;
  5900. case -NFS4ERR_STALE_CLIENTID:
  5901. case -NFS4ERR_EXPIRED:
  5902. nfs4_schedule_lease_recovery(server->nfs_client);
  5903. break;
  5904. case -NFS4ERR_LEASE_MOVED:
  5905. case -NFS4ERR_DELAY:
  5906. if (nfs4_async_handle_error(task, server,
  5907. NULL, NULL) == -EAGAIN)
  5908. rpc_restart_call_prepare(task);
  5909. }
  5910. }
  5911. static void nfs4_release_lockowner_release(void *calldata)
  5912. {
  5913. struct nfs_release_lockowner_data *data = calldata;
  5914. nfs4_free_lock_state(data->server, data->lsp);
  5915. kfree(calldata);
  5916. }
  5917. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  5918. .rpc_call_prepare = nfs4_release_lockowner_prepare,
  5919. .rpc_call_done = nfs4_release_lockowner_done,
  5920. .rpc_release = nfs4_release_lockowner_release,
  5921. };
  5922. static void
  5923. nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
  5924. {
  5925. struct nfs_release_lockowner_data *data;
  5926. struct rpc_message msg = {
  5927. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  5928. };
  5929. if (server->nfs_client->cl_mvops->minor_version != 0)
  5930. return;
  5931. data = kmalloc(sizeof(*data), GFP_NOFS);
  5932. if (!data)
  5933. return;
  5934. data->lsp = lsp;
  5935. data->server = server;
  5936. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  5937. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  5938. data->args.lock_owner.s_dev = server->s_dev;
  5939. msg.rpc_argp = &data->args;
  5940. msg.rpc_resp = &data->res;
  5941. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  5942. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  5943. }
  5944. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  5945. static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
  5946. struct dentry *unused, struct inode *inode,
  5947. const char *key, const void *buf,
  5948. size_t buflen, int flags)
  5949. {
  5950. return nfs4_proc_set_acl(inode, buf, buflen);
  5951. }
  5952. static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
  5953. struct dentry *unused, struct inode *inode,
  5954. const char *key, void *buf, size_t buflen)
  5955. {
  5956. return nfs4_proc_get_acl(inode, buf, buflen);
  5957. }
  5958. static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
  5959. {
  5960. return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
  5961. }
  5962. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  5963. static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
  5964. struct dentry *unused, struct inode *inode,
  5965. const char *key, const void *buf,
  5966. size_t buflen, int flags)
  5967. {
  5968. if (security_ismaclabel(key))
  5969. return nfs4_set_security_label(inode, buf, buflen);
  5970. return -EOPNOTSUPP;
  5971. }
  5972. static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
  5973. struct dentry *unused, struct inode *inode,
  5974. const char *key, void *buf, size_t buflen)
  5975. {
  5976. if (security_ismaclabel(key))
  5977. return nfs4_get_security_label(inode, buf, buflen);
  5978. return -EOPNOTSUPP;
  5979. }
  5980. static ssize_t
  5981. nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
  5982. {
  5983. int len = 0;
  5984. if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
  5985. len = security_inode_listsecurity(inode, list, list_len);
  5986. if (list_len && len > list_len)
  5987. return -ERANGE;
  5988. }
  5989. return len;
  5990. }
  5991. static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
  5992. .prefix = XATTR_SECURITY_PREFIX,
  5993. .get = nfs4_xattr_get_nfs4_label,
  5994. .set = nfs4_xattr_set_nfs4_label,
  5995. };
  5996. #else
  5997. static ssize_t
  5998. nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
  5999. {
  6000. return 0;
  6001. }
  6002. #endif
  6003. /*
  6004. * nfs_fhget will use either the mounted_on_fileid or the fileid
  6005. */
  6006. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  6007. {
  6008. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  6009. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  6010. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  6011. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  6012. return;
  6013. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  6014. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  6015. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  6016. fattr->nlink = 2;
  6017. }
  6018. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  6019. const struct qstr *name,
  6020. struct nfs4_fs_locations *fs_locations,
  6021. struct page *page)
  6022. {
  6023. struct nfs_server *server = NFS_SERVER(dir);
  6024. u32 bitmask[3] = {
  6025. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6026. };
  6027. struct nfs4_fs_locations_arg args = {
  6028. .dir_fh = NFS_FH(dir),
  6029. .name = name,
  6030. .page = page,
  6031. .bitmask = bitmask,
  6032. };
  6033. struct nfs4_fs_locations_res res = {
  6034. .fs_locations = fs_locations,
  6035. };
  6036. struct rpc_message msg = {
  6037. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6038. .rpc_argp = &args,
  6039. .rpc_resp = &res,
  6040. };
  6041. int status;
  6042. dprintk("%s: start\n", __func__);
  6043. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  6044. * is not supported */
  6045. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  6046. bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
  6047. else
  6048. bitmask[0] |= FATTR4_WORD0_FILEID;
  6049. nfs_fattr_init(&fs_locations->fattr);
  6050. fs_locations->server = server;
  6051. fs_locations->nlocations = 0;
  6052. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  6053. dprintk("%s: returned status = %d\n", __func__, status);
  6054. return status;
  6055. }
  6056. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  6057. const struct qstr *name,
  6058. struct nfs4_fs_locations *fs_locations,
  6059. struct page *page)
  6060. {
  6061. struct nfs4_exception exception = { };
  6062. int err;
  6063. do {
  6064. err = _nfs4_proc_fs_locations(client, dir, name,
  6065. fs_locations, page);
  6066. trace_nfs4_get_fs_locations(dir, name, err);
  6067. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  6068. &exception);
  6069. } while (exception.retry);
  6070. return err;
  6071. }
  6072. /*
  6073. * This operation also signals the server that this client is
  6074. * performing migration recovery. The server can stop returning
  6075. * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
  6076. * appended to this compound to identify the client ID which is
  6077. * performing recovery.
  6078. */
  6079. static int _nfs40_proc_get_locations(struct inode *inode,
  6080. struct nfs4_fs_locations *locations,
  6081. struct page *page, struct rpc_cred *cred)
  6082. {
  6083. struct nfs_server *server = NFS_SERVER(inode);
  6084. struct rpc_clnt *clnt = server->client;
  6085. u32 bitmask[2] = {
  6086. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6087. };
  6088. struct nfs4_fs_locations_arg args = {
  6089. .clientid = server->nfs_client->cl_clientid,
  6090. .fh = NFS_FH(inode),
  6091. .page = page,
  6092. .bitmask = bitmask,
  6093. .migration = 1, /* skip LOOKUP */
  6094. .renew = 1, /* append RENEW */
  6095. };
  6096. struct nfs4_fs_locations_res res = {
  6097. .fs_locations = locations,
  6098. .migration = 1,
  6099. .renew = 1,
  6100. };
  6101. struct rpc_message msg = {
  6102. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6103. .rpc_argp = &args,
  6104. .rpc_resp = &res,
  6105. .rpc_cred = cred,
  6106. };
  6107. unsigned long now = jiffies;
  6108. int status;
  6109. nfs_fattr_init(&locations->fattr);
  6110. locations->server = server;
  6111. locations->nlocations = 0;
  6112. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6113. nfs4_set_sequence_privileged(&args.seq_args);
  6114. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6115. &args.seq_args, &res.seq_res);
  6116. if (status)
  6117. return status;
  6118. renew_lease(server, now);
  6119. return 0;
  6120. }
  6121. #ifdef CONFIG_NFS_V4_1
  6122. /*
  6123. * This operation also signals the server that this client is
  6124. * performing migration recovery. The server can stop asserting
  6125. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
  6126. * performing this operation is identified in the SEQUENCE
  6127. * operation in this compound.
  6128. *
  6129. * When the client supports GETATTR(fs_locations_info), it can
  6130. * be plumbed in here.
  6131. */
  6132. static int _nfs41_proc_get_locations(struct inode *inode,
  6133. struct nfs4_fs_locations *locations,
  6134. struct page *page, struct rpc_cred *cred)
  6135. {
  6136. struct nfs_server *server = NFS_SERVER(inode);
  6137. struct rpc_clnt *clnt = server->client;
  6138. u32 bitmask[2] = {
  6139. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6140. };
  6141. struct nfs4_fs_locations_arg args = {
  6142. .fh = NFS_FH(inode),
  6143. .page = page,
  6144. .bitmask = bitmask,
  6145. .migration = 1, /* skip LOOKUP */
  6146. };
  6147. struct nfs4_fs_locations_res res = {
  6148. .fs_locations = locations,
  6149. .migration = 1,
  6150. };
  6151. struct rpc_message msg = {
  6152. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6153. .rpc_argp = &args,
  6154. .rpc_resp = &res,
  6155. .rpc_cred = cred,
  6156. };
  6157. int status;
  6158. nfs_fattr_init(&locations->fattr);
  6159. locations->server = server;
  6160. locations->nlocations = 0;
  6161. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6162. nfs4_set_sequence_privileged(&args.seq_args);
  6163. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6164. &args.seq_args, &res.seq_res);
  6165. if (status == NFS4_OK &&
  6166. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  6167. status = -NFS4ERR_LEASE_MOVED;
  6168. return status;
  6169. }
  6170. #endif /* CONFIG_NFS_V4_1 */
  6171. /**
  6172. * nfs4_proc_get_locations - discover locations for a migrated FSID
  6173. * @inode: inode on FSID that is migrating
  6174. * @locations: result of query
  6175. * @page: buffer
  6176. * @cred: credential to use for this operation
  6177. *
  6178. * Returns NFS4_OK on success, a negative NFS4ERR status code if the
  6179. * operation failed, or a negative errno if a local error occurred.
  6180. *
  6181. * On success, "locations" is filled in, but if the server has
  6182. * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
  6183. * asserted.
  6184. *
  6185. * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
  6186. * from this client that require migration recovery.
  6187. */
  6188. int nfs4_proc_get_locations(struct inode *inode,
  6189. struct nfs4_fs_locations *locations,
  6190. struct page *page, struct rpc_cred *cred)
  6191. {
  6192. struct nfs_server *server = NFS_SERVER(inode);
  6193. struct nfs_client *clp = server->nfs_client;
  6194. const struct nfs4_mig_recovery_ops *ops =
  6195. clp->cl_mvops->mig_recovery_ops;
  6196. struct nfs4_exception exception = { };
  6197. int status;
  6198. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  6199. (unsigned long long)server->fsid.major,
  6200. (unsigned long long)server->fsid.minor,
  6201. clp->cl_hostname);
  6202. nfs_display_fhandle(NFS_FH(inode), __func__);
  6203. do {
  6204. status = ops->get_locations(inode, locations, page, cred);
  6205. if (status != -NFS4ERR_DELAY)
  6206. break;
  6207. nfs4_handle_exception(server, status, &exception);
  6208. } while (exception.retry);
  6209. return status;
  6210. }
  6211. /*
  6212. * This operation also signals the server that this client is
  6213. * performing "lease moved" recovery. The server can stop
  6214. * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
  6215. * is appended to this compound to identify the client ID which is
  6216. * performing recovery.
  6217. */
  6218. static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6219. {
  6220. struct nfs_server *server = NFS_SERVER(inode);
  6221. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  6222. struct rpc_clnt *clnt = server->client;
  6223. struct nfs4_fsid_present_arg args = {
  6224. .fh = NFS_FH(inode),
  6225. .clientid = clp->cl_clientid,
  6226. .renew = 1, /* append RENEW */
  6227. };
  6228. struct nfs4_fsid_present_res res = {
  6229. .renew = 1,
  6230. };
  6231. struct rpc_message msg = {
  6232. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  6233. .rpc_argp = &args,
  6234. .rpc_resp = &res,
  6235. .rpc_cred = cred,
  6236. };
  6237. unsigned long now = jiffies;
  6238. int status;
  6239. res.fh = nfs_alloc_fhandle();
  6240. if (res.fh == NULL)
  6241. return -ENOMEM;
  6242. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6243. nfs4_set_sequence_privileged(&args.seq_args);
  6244. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6245. &args.seq_args, &res.seq_res);
  6246. nfs_free_fhandle(res.fh);
  6247. if (status)
  6248. return status;
  6249. do_renew_lease(clp, now);
  6250. return 0;
  6251. }
  6252. #ifdef CONFIG_NFS_V4_1
  6253. /*
  6254. * This operation also signals the server that this client is
  6255. * performing "lease moved" recovery. The server can stop asserting
  6256. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
  6257. * this operation is identified in the SEQUENCE operation in this
  6258. * compound.
  6259. */
  6260. static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6261. {
  6262. struct nfs_server *server = NFS_SERVER(inode);
  6263. struct rpc_clnt *clnt = server->client;
  6264. struct nfs4_fsid_present_arg args = {
  6265. .fh = NFS_FH(inode),
  6266. };
  6267. struct nfs4_fsid_present_res res = {
  6268. };
  6269. struct rpc_message msg = {
  6270. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  6271. .rpc_argp = &args,
  6272. .rpc_resp = &res,
  6273. .rpc_cred = cred,
  6274. };
  6275. int status;
  6276. res.fh = nfs_alloc_fhandle();
  6277. if (res.fh == NULL)
  6278. return -ENOMEM;
  6279. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6280. nfs4_set_sequence_privileged(&args.seq_args);
  6281. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6282. &args.seq_args, &res.seq_res);
  6283. nfs_free_fhandle(res.fh);
  6284. if (status == NFS4_OK &&
  6285. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  6286. status = -NFS4ERR_LEASE_MOVED;
  6287. return status;
  6288. }
  6289. #endif /* CONFIG_NFS_V4_1 */
  6290. /**
  6291. * nfs4_proc_fsid_present - Is this FSID present or absent on server?
  6292. * @inode: inode on FSID to check
  6293. * @cred: credential to use for this operation
  6294. *
  6295. * Server indicates whether the FSID is present, moved, or not
  6296. * recognized. This operation is necessary to clear a LEASE_MOVED
  6297. * condition for this client ID.
  6298. *
  6299. * Returns NFS4_OK if the FSID is present on this server,
  6300. * -NFS4ERR_MOVED if the FSID is no longer present, a negative
  6301. * NFS4ERR code if some error occurred on the server, or a
  6302. * negative errno if a local failure occurred.
  6303. */
  6304. int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6305. {
  6306. struct nfs_server *server = NFS_SERVER(inode);
  6307. struct nfs_client *clp = server->nfs_client;
  6308. const struct nfs4_mig_recovery_ops *ops =
  6309. clp->cl_mvops->mig_recovery_ops;
  6310. struct nfs4_exception exception = { };
  6311. int status;
  6312. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  6313. (unsigned long long)server->fsid.major,
  6314. (unsigned long long)server->fsid.minor,
  6315. clp->cl_hostname);
  6316. nfs_display_fhandle(NFS_FH(inode), __func__);
  6317. do {
  6318. status = ops->fsid_present(inode, cred);
  6319. if (status != -NFS4ERR_DELAY)
  6320. break;
  6321. nfs4_handle_exception(server, status, &exception);
  6322. } while (exception.retry);
  6323. return status;
  6324. }
  6325. /**
  6326. * If 'use_integrity' is true and the state managment nfs_client
  6327. * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
  6328. * and the machine credential as per RFC3530bis and RFC5661 Security
  6329. * Considerations sections. Otherwise, just use the user cred with the
  6330. * filesystem's rpc_client.
  6331. */
  6332. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  6333. {
  6334. int status;
  6335. struct nfs4_secinfo_arg args = {
  6336. .dir_fh = NFS_FH(dir),
  6337. .name = name,
  6338. };
  6339. struct nfs4_secinfo_res res = {
  6340. .flavors = flavors,
  6341. };
  6342. struct rpc_message msg = {
  6343. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  6344. .rpc_argp = &args,
  6345. .rpc_resp = &res,
  6346. };
  6347. struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
  6348. struct rpc_cred *cred = NULL;
  6349. if (use_integrity) {
  6350. clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
  6351. cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
  6352. msg.rpc_cred = cred;
  6353. }
  6354. dprintk("NFS call secinfo %s\n", name->name);
  6355. nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
  6356. NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
  6357. status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
  6358. &res.seq_res, 0);
  6359. dprintk("NFS reply secinfo: %d\n", status);
  6360. if (cred)
  6361. put_rpccred(cred);
  6362. return status;
  6363. }
  6364. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  6365. struct nfs4_secinfo_flavors *flavors)
  6366. {
  6367. struct nfs4_exception exception = { };
  6368. int err;
  6369. do {
  6370. err = -NFS4ERR_WRONGSEC;
  6371. /* try to use integrity protection with machine cred */
  6372. if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
  6373. err = _nfs4_proc_secinfo(dir, name, flavors, true);
  6374. /*
  6375. * if unable to use integrity protection, or SECINFO with
  6376. * integrity protection returns NFS4ERR_WRONGSEC (which is
  6377. * disallowed by spec, but exists in deployed servers) use
  6378. * the current filesystem's rpc_client and the user cred.
  6379. */
  6380. if (err == -NFS4ERR_WRONGSEC)
  6381. err = _nfs4_proc_secinfo(dir, name, flavors, false);
  6382. trace_nfs4_secinfo(dir, name, err);
  6383. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  6384. &exception);
  6385. } while (exception.retry);
  6386. return err;
  6387. }
  6388. #ifdef CONFIG_NFS_V4_1
  6389. /*
  6390. * Check the exchange flags returned by the server for invalid flags, having
  6391. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  6392. * DS flags set.
  6393. */
  6394. static int nfs4_check_cl_exchange_flags(u32 flags)
  6395. {
  6396. if (flags & ~EXCHGID4_FLAG_MASK_R)
  6397. goto out_inval;
  6398. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  6399. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  6400. goto out_inval;
  6401. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  6402. goto out_inval;
  6403. return NFS_OK;
  6404. out_inval:
  6405. return -NFS4ERR_INVAL;
  6406. }
  6407. static bool
  6408. nfs41_same_server_scope(struct nfs41_server_scope *a,
  6409. struct nfs41_server_scope *b)
  6410. {
  6411. if (a->server_scope_sz != b->server_scope_sz)
  6412. return false;
  6413. return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
  6414. }
  6415. static void
  6416. nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
  6417. {
  6418. }
  6419. static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
  6420. .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
  6421. };
  6422. /*
  6423. * nfs4_proc_bind_one_conn_to_session()
  6424. *
  6425. * The 4.1 client currently uses the same TCP connection for the
  6426. * fore and backchannel.
  6427. */
  6428. static
  6429. int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
  6430. struct rpc_xprt *xprt,
  6431. struct nfs_client *clp,
  6432. struct rpc_cred *cred)
  6433. {
  6434. int status;
  6435. struct nfs41_bind_conn_to_session_args args = {
  6436. .client = clp,
  6437. .dir = NFS4_CDFC4_FORE_OR_BOTH,
  6438. };
  6439. struct nfs41_bind_conn_to_session_res res;
  6440. struct rpc_message msg = {
  6441. .rpc_proc =
  6442. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  6443. .rpc_argp = &args,
  6444. .rpc_resp = &res,
  6445. .rpc_cred = cred,
  6446. };
  6447. struct rpc_task_setup task_setup_data = {
  6448. .rpc_client = clnt,
  6449. .rpc_xprt = xprt,
  6450. .callback_ops = &nfs4_bind_one_conn_to_session_ops,
  6451. .rpc_message = &msg,
  6452. .flags = RPC_TASK_TIMEOUT,
  6453. };
  6454. struct rpc_task *task;
  6455. nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
  6456. if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
  6457. args.dir = NFS4_CDFC4_FORE;
  6458. /* Do not set the backchannel flag unless this is clnt->cl_xprt */
  6459. if (xprt != rcu_access_pointer(clnt->cl_xprt))
  6460. args.dir = NFS4_CDFC4_FORE;
  6461. task = rpc_run_task(&task_setup_data);
  6462. if (!IS_ERR(task)) {
  6463. status = task->tk_status;
  6464. rpc_put_task(task);
  6465. } else
  6466. status = PTR_ERR(task);
  6467. trace_nfs4_bind_conn_to_session(clp, status);
  6468. if (status == 0) {
  6469. if (memcmp(res.sessionid.data,
  6470. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  6471. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  6472. return -EIO;
  6473. }
  6474. if ((res.dir & args.dir) != res.dir || res.dir == 0) {
  6475. dprintk("NFS: %s: Unexpected direction from server\n",
  6476. __func__);
  6477. return -EIO;
  6478. }
  6479. if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
  6480. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  6481. __func__);
  6482. return -EIO;
  6483. }
  6484. }
  6485. return status;
  6486. }
  6487. struct rpc_bind_conn_calldata {
  6488. struct nfs_client *clp;
  6489. struct rpc_cred *cred;
  6490. };
  6491. static int
  6492. nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
  6493. struct rpc_xprt *xprt,
  6494. void *calldata)
  6495. {
  6496. struct rpc_bind_conn_calldata *p = calldata;
  6497. return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
  6498. }
  6499. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
  6500. {
  6501. struct rpc_bind_conn_calldata data = {
  6502. .clp = clp,
  6503. .cred = cred,
  6504. };
  6505. return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
  6506. nfs4_proc_bind_conn_to_session_callback, &data);
  6507. }
  6508. /*
  6509. * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
  6510. * and operations we'd like to see to enable certain features in the allow map
  6511. */
  6512. static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
  6513. .how = SP4_MACH_CRED,
  6514. .enforce.u.words = {
  6515. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  6516. 1 << (OP_EXCHANGE_ID - 32) |
  6517. 1 << (OP_CREATE_SESSION - 32) |
  6518. 1 << (OP_DESTROY_SESSION - 32) |
  6519. 1 << (OP_DESTROY_CLIENTID - 32)
  6520. },
  6521. .allow.u.words = {
  6522. [0] = 1 << (OP_CLOSE) |
  6523. 1 << (OP_OPEN_DOWNGRADE) |
  6524. 1 << (OP_LOCKU) |
  6525. 1 << (OP_DELEGRETURN) |
  6526. 1 << (OP_COMMIT),
  6527. [1] = 1 << (OP_SECINFO - 32) |
  6528. 1 << (OP_SECINFO_NO_NAME - 32) |
  6529. 1 << (OP_LAYOUTRETURN - 32) |
  6530. 1 << (OP_TEST_STATEID - 32) |
  6531. 1 << (OP_FREE_STATEID - 32) |
  6532. 1 << (OP_WRITE - 32)
  6533. }
  6534. };
  6535. /*
  6536. * Select the state protection mode for client `clp' given the server results
  6537. * from exchange_id in `sp'.
  6538. *
  6539. * Returns 0 on success, negative errno otherwise.
  6540. */
  6541. static int nfs4_sp4_select_mode(struct nfs_client *clp,
  6542. struct nfs41_state_protection *sp)
  6543. {
  6544. static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
  6545. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  6546. 1 << (OP_EXCHANGE_ID - 32) |
  6547. 1 << (OP_CREATE_SESSION - 32) |
  6548. 1 << (OP_DESTROY_SESSION - 32) |
  6549. 1 << (OP_DESTROY_CLIENTID - 32)
  6550. };
  6551. unsigned int i;
  6552. if (sp->how == SP4_MACH_CRED) {
  6553. /* Print state protect result */
  6554. dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
  6555. for (i = 0; i <= LAST_NFS4_OP; i++) {
  6556. if (test_bit(i, sp->enforce.u.longs))
  6557. dfprintk(MOUNT, " enforce op %d\n", i);
  6558. if (test_bit(i, sp->allow.u.longs))
  6559. dfprintk(MOUNT, " allow op %d\n", i);
  6560. }
  6561. /* make sure nothing is on enforce list that isn't supported */
  6562. for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
  6563. if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
  6564. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  6565. return -EINVAL;
  6566. }
  6567. }
  6568. /*
  6569. * Minimal mode - state operations are allowed to use machine
  6570. * credential. Note this already happens by default, so the
  6571. * client doesn't have to do anything more than the negotiation.
  6572. *
  6573. * NOTE: we don't care if EXCHANGE_ID is in the list -
  6574. * we're already using the machine cred for exchange_id
  6575. * and will never use a different cred.
  6576. */
  6577. if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
  6578. test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
  6579. test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
  6580. test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
  6581. dfprintk(MOUNT, "sp4_mach_cred:\n");
  6582. dfprintk(MOUNT, " minimal mode enabled\n");
  6583. set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
  6584. } else {
  6585. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  6586. return -EINVAL;
  6587. }
  6588. if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
  6589. test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
  6590. test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
  6591. test_bit(OP_LOCKU, sp->allow.u.longs)) {
  6592. dfprintk(MOUNT, " cleanup mode enabled\n");
  6593. set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
  6594. }
  6595. if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
  6596. dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
  6597. set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
  6598. &clp->cl_sp4_flags);
  6599. }
  6600. if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
  6601. test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
  6602. dfprintk(MOUNT, " secinfo mode enabled\n");
  6603. set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
  6604. }
  6605. if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
  6606. test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
  6607. dfprintk(MOUNT, " stateid mode enabled\n");
  6608. set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
  6609. }
  6610. if (test_bit(OP_WRITE, sp->allow.u.longs)) {
  6611. dfprintk(MOUNT, " write mode enabled\n");
  6612. set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
  6613. }
  6614. if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
  6615. dfprintk(MOUNT, " commit mode enabled\n");
  6616. set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
  6617. }
  6618. }
  6619. return 0;
  6620. }
  6621. struct nfs41_exchange_id_data {
  6622. struct nfs41_exchange_id_res res;
  6623. struct nfs41_exchange_id_args args;
  6624. struct rpc_xprt *xprt;
  6625. int rpc_status;
  6626. };
  6627. static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
  6628. {
  6629. struct nfs41_exchange_id_data *cdata =
  6630. (struct nfs41_exchange_id_data *)data;
  6631. struct nfs_client *clp = cdata->args.client;
  6632. int status = task->tk_status;
  6633. trace_nfs4_exchange_id(clp, status);
  6634. if (status == 0)
  6635. status = nfs4_check_cl_exchange_flags(cdata->res.flags);
  6636. if (cdata->xprt && status == 0) {
  6637. status = nfs4_detect_session_trunking(clp, &cdata->res,
  6638. cdata->xprt);
  6639. goto out;
  6640. }
  6641. if (status == 0)
  6642. status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
  6643. if (status == 0) {
  6644. clp->cl_clientid = cdata->res.clientid;
  6645. clp->cl_exchange_flags = cdata->res.flags;
  6646. clp->cl_seqid = cdata->res.seqid;
  6647. /* Client ID is not confirmed */
  6648. if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R))
  6649. clear_bit(NFS4_SESSION_ESTABLISHED,
  6650. &clp->cl_session->session_state);
  6651. kfree(clp->cl_serverowner);
  6652. clp->cl_serverowner = cdata->res.server_owner;
  6653. cdata->res.server_owner = NULL;
  6654. /* use the most recent implementation id */
  6655. kfree(clp->cl_implid);
  6656. clp->cl_implid = cdata->res.impl_id;
  6657. cdata->res.impl_id = NULL;
  6658. if (clp->cl_serverscope != NULL &&
  6659. !nfs41_same_server_scope(clp->cl_serverscope,
  6660. cdata->res.server_scope)) {
  6661. dprintk("%s: server_scope mismatch detected\n",
  6662. __func__);
  6663. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  6664. kfree(clp->cl_serverscope);
  6665. clp->cl_serverscope = NULL;
  6666. }
  6667. if (clp->cl_serverscope == NULL) {
  6668. clp->cl_serverscope = cdata->res.server_scope;
  6669. cdata->res.server_scope = NULL;
  6670. }
  6671. /* Save the EXCHANGE_ID verifier session trunk tests */
  6672. memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
  6673. sizeof(clp->cl_confirm.data));
  6674. }
  6675. out:
  6676. cdata->rpc_status = status;
  6677. return;
  6678. }
  6679. static void nfs4_exchange_id_release(void *data)
  6680. {
  6681. struct nfs41_exchange_id_data *cdata =
  6682. (struct nfs41_exchange_id_data *)data;
  6683. if (cdata->xprt) {
  6684. xprt_put(cdata->xprt);
  6685. rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
  6686. }
  6687. nfs_put_client(cdata->args.client);
  6688. kfree(cdata->res.impl_id);
  6689. kfree(cdata->res.server_scope);
  6690. kfree(cdata->res.server_owner);
  6691. kfree(cdata);
  6692. }
  6693. static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
  6694. .rpc_call_done = nfs4_exchange_id_done,
  6695. .rpc_release = nfs4_exchange_id_release,
  6696. };
  6697. /*
  6698. * _nfs4_proc_exchange_id()
  6699. *
  6700. * Wrapper for EXCHANGE_ID operation.
  6701. */
  6702. static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
  6703. u32 sp4_how, struct rpc_xprt *xprt)
  6704. {
  6705. nfs4_verifier verifier;
  6706. struct rpc_message msg = {
  6707. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  6708. .rpc_cred = cred,
  6709. };
  6710. struct rpc_task_setup task_setup_data = {
  6711. .rpc_client = clp->cl_rpcclient,
  6712. .callback_ops = &nfs4_exchange_id_call_ops,
  6713. .rpc_message = &msg,
  6714. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
  6715. };
  6716. struct nfs41_exchange_id_data *calldata;
  6717. struct rpc_task *task;
  6718. int status;
  6719. if (!atomic_inc_not_zero(&clp->cl_count))
  6720. return -EIO;
  6721. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  6722. if (!calldata) {
  6723. nfs_put_client(clp);
  6724. return -ENOMEM;
  6725. }
  6726. if (!xprt)
  6727. nfs4_init_boot_verifier(clp, &verifier);
  6728. status = nfs4_init_uniform_client_string(clp);
  6729. if (status)
  6730. goto out_calldata;
  6731. calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
  6732. GFP_NOFS);
  6733. status = -ENOMEM;
  6734. if (unlikely(calldata->res.server_owner == NULL))
  6735. goto out_calldata;
  6736. calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
  6737. GFP_NOFS);
  6738. if (unlikely(calldata->res.server_scope == NULL))
  6739. goto out_server_owner;
  6740. calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
  6741. if (unlikely(calldata->res.impl_id == NULL))
  6742. goto out_server_scope;
  6743. switch (sp4_how) {
  6744. case SP4_NONE:
  6745. calldata->args.state_protect.how = SP4_NONE;
  6746. break;
  6747. case SP4_MACH_CRED:
  6748. calldata->args.state_protect = nfs4_sp4_mach_cred_request;
  6749. break;
  6750. default:
  6751. /* unsupported! */
  6752. WARN_ON_ONCE(1);
  6753. status = -EINVAL;
  6754. goto out_impl_id;
  6755. }
  6756. if (xprt) {
  6757. calldata->xprt = xprt;
  6758. task_setup_data.rpc_xprt = xprt;
  6759. task_setup_data.flags =
  6760. RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
  6761. calldata->args.verifier = &clp->cl_confirm;
  6762. } else {
  6763. calldata->args.verifier = &verifier;
  6764. }
  6765. calldata->args.client = clp;
  6766. #ifdef CONFIG_NFS_V4_1_MIGRATION
  6767. calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
  6768. EXCHGID4_FLAG_BIND_PRINC_STATEID |
  6769. EXCHGID4_FLAG_SUPP_MOVED_MIGR,
  6770. #else
  6771. calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
  6772. EXCHGID4_FLAG_BIND_PRINC_STATEID,
  6773. #endif
  6774. msg.rpc_argp = &calldata->args;
  6775. msg.rpc_resp = &calldata->res;
  6776. task_setup_data.callback_data = calldata;
  6777. task = rpc_run_task(&task_setup_data);
  6778. if (IS_ERR(task))
  6779. return PTR_ERR(task);
  6780. if (!xprt) {
  6781. status = rpc_wait_for_completion_task(task);
  6782. if (!status)
  6783. status = calldata->rpc_status;
  6784. } else /* session trunking test */
  6785. status = calldata->rpc_status;
  6786. rpc_put_task(task);
  6787. out:
  6788. return status;
  6789. out_impl_id:
  6790. kfree(calldata->res.impl_id);
  6791. out_server_scope:
  6792. kfree(calldata->res.server_scope);
  6793. out_server_owner:
  6794. kfree(calldata->res.server_owner);
  6795. out_calldata:
  6796. kfree(calldata);
  6797. nfs_put_client(clp);
  6798. goto out;
  6799. }
  6800. /*
  6801. * nfs4_proc_exchange_id()
  6802. *
  6803. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  6804. *
  6805. * Since the clientid has expired, all compounds using sessions
  6806. * associated with the stale clientid will be returning
  6807. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  6808. * be in some phase of session reset.
  6809. *
  6810. * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
  6811. */
  6812. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  6813. {
  6814. rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
  6815. int status;
  6816. /* try SP4_MACH_CRED if krb5i/p */
  6817. if (authflavor == RPC_AUTH_GSS_KRB5I ||
  6818. authflavor == RPC_AUTH_GSS_KRB5P) {
  6819. status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
  6820. if (!status)
  6821. return 0;
  6822. }
  6823. /* try SP4_NONE */
  6824. return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
  6825. }
  6826. /**
  6827. * nfs4_test_session_trunk
  6828. *
  6829. * This is an add_xprt_test() test function called from
  6830. * rpc_clnt_setup_test_and_add_xprt.
  6831. *
  6832. * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
  6833. * and is dereferrenced in nfs4_exchange_id_release
  6834. *
  6835. * Upon success, add the new transport to the rpc_clnt
  6836. *
  6837. * @clnt: struct rpc_clnt to get new transport
  6838. * @xprt: the rpc_xprt to test
  6839. * @data: call data for _nfs4_proc_exchange_id.
  6840. */
  6841. int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
  6842. void *data)
  6843. {
  6844. struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
  6845. u32 sp4_how;
  6846. dprintk("--> %s try %s\n", __func__,
  6847. xprt->address_strings[RPC_DISPLAY_ADDR]);
  6848. sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
  6849. /* Test connection for session trunking. Async exchange_id call */
  6850. return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
  6851. }
  6852. EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
  6853. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  6854. struct rpc_cred *cred)
  6855. {
  6856. struct rpc_message msg = {
  6857. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  6858. .rpc_argp = clp,
  6859. .rpc_cred = cred,
  6860. };
  6861. int status;
  6862. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  6863. trace_nfs4_destroy_clientid(clp, status);
  6864. if (status)
  6865. dprintk("NFS: Got error %d from the server %s on "
  6866. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  6867. return status;
  6868. }
  6869. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  6870. struct rpc_cred *cred)
  6871. {
  6872. unsigned int loop;
  6873. int ret;
  6874. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  6875. ret = _nfs4_proc_destroy_clientid(clp, cred);
  6876. switch (ret) {
  6877. case -NFS4ERR_DELAY:
  6878. case -NFS4ERR_CLIENTID_BUSY:
  6879. ssleep(1);
  6880. break;
  6881. default:
  6882. return ret;
  6883. }
  6884. }
  6885. return 0;
  6886. }
  6887. int nfs4_destroy_clientid(struct nfs_client *clp)
  6888. {
  6889. struct rpc_cred *cred;
  6890. int ret = 0;
  6891. if (clp->cl_mvops->minor_version < 1)
  6892. goto out;
  6893. if (clp->cl_exchange_flags == 0)
  6894. goto out;
  6895. if (clp->cl_preserve_clid)
  6896. goto out;
  6897. cred = nfs4_get_clid_cred(clp);
  6898. ret = nfs4_proc_destroy_clientid(clp, cred);
  6899. if (cred)
  6900. put_rpccred(cred);
  6901. switch (ret) {
  6902. case 0:
  6903. case -NFS4ERR_STALE_CLIENTID:
  6904. clp->cl_exchange_flags = 0;
  6905. }
  6906. out:
  6907. return ret;
  6908. }
  6909. struct nfs4_get_lease_time_data {
  6910. struct nfs4_get_lease_time_args *args;
  6911. struct nfs4_get_lease_time_res *res;
  6912. struct nfs_client *clp;
  6913. };
  6914. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  6915. void *calldata)
  6916. {
  6917. struct nfs4_get_lease_time_data *data =
  6918. (struct nfs4_get_lease_time_data *)calldata;
  6919. dprintk("--> %s\n", __func__);
  6920. /* just setup sequence, do not trigger session recovery
  6921. since we're invoked within one */
  6922. nfs4_setup_sequence(data->clp,
  6923. &data->args->la_seq_args,
  6924. &data->res->lr_seq_res,
  6925. task);
  6926. dprintk("<-- %s\n", __func__);
  6927. }
  6928. /*
  6929. * Called from nfs4_state_manager thread for session setup, so don't recover
  6930. * from sequence operation or clientid errors.
  6931. */
  6932. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  6933. {
  6934. struct nfs4_get_lease_time_data *data =
  6935. (struct nfs4_get_lease_time_data *)calldata;
  6936. dprintk("--> %s\n", __func__);
  6937. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  6938. return;
  6939. switch (task->tk_status) {
  6940. case -NFS4ERR_DELAY:
  6941. case -NFS4ERR_GRACE:
  6942. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  6943. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  6944. task->tk_status = 0;
  6945. /* fall through */
  6946. case -NFS4ERR_RETRY_UNCACHED_REP:
  6947. rpc_restart_call_prepare(task);
  6948. return;
  6949. }
  6950. dprintk("<-- %s\n", __func__);
  6951. }
  6952. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  6953. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  6954. .rpc_call_done = nfs4_get_lease_time_done,
  6955. };
  6956. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  6957. {
  6958. struct rpc_task *task;
  6959. struct nfs4_get_lease_time_args args;
  6960. struct nfs4_get_lease_time_res res = {
  6961. .lr_fsinfo = fsinfo,
  6962. };
  6963. struct nfs4_get_lease_time_data data = {
  6964. .args = &args,
  6965. .res = &res,
  6966. .clp = clp,
  6967. };
  6968. struct rpc_message msg = {
  6969. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  6970. .rpc_argp = &args,
  6971. .rpc_resp = &res,
  6972. };
  6973. struct rpc_task_setup task_setup = {
  6974. .rpc_client = clp->cl_rpcclient,
  6975. .rpc_message = &msg,
  6976. .callback_ops = &nfs4_get_lease_time_ops,
  6977. .callback_data = &data,
  6978. .flags = RPC_TASK_TIMEOUT,
  6979. };
  6980. int status;
  6981. nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
  6982. nfs4_set_sequence_privileged(&args.la_seq_args);
  6983. task = rpc_run_task(&task_setup);
  6984. if (IS_ERR(task))
  6985. return PTR_ERR(task);
  6986. status = task->tk_status;
  6987. rpc_put_task(task);
  6988. return status;
  6989. }
  6990. /*
  6991. * Initialize the values to be used by the client in CREATE_SESSION
  6992. * If nfs4_init_session set the fore channel request and response sizes,
  6993. * use them.
  6994. *
  6995. * Set the back channel max_resp_sz_cached to zero to force the client to
  6996. * always set csa_cachethis to FALSE because the current implementation
  6997. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  6998. */
  6999. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
  7000. struct rpc_clnt *clnt)
  7001. {
  7002. unsigned int max_rqst_sz, max_resp_sz;
  7003. unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
  7004. max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
  7005. max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
  7006. /* Fore channel attributes */
  7007. args->fc_attrs.max_rqst_sz = max_rqst_sz;
  7008. args->fc_attrs.max_resp_sz = max_resp_sz;
  7009. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  7010. args->fc_attrs.max_reqs = max_session_slots;
  7011. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  7012. "max_ops=%u max_reqs=%u\n",
  7013. __func__,
  7014. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  7015. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  7016. /* Back channel attributes */
  7017. args->bc_attrs.max_rqst_sz = max_bc_payload;
  7018. args->bc_attrs.max_resp_sz = max_bc_payload;
  7019. args->bc_attrs.max_resp_sz_cached = 0;
  7020. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  7021. args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
  7022. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  7023. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  7024. __func__,
  7025. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  7026. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  7027. args->bc_attrs.max_reqs);
  7028. }
  7029. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
  7030. struct nfs41_create_session_res *res)
  7031. {
  7032. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  7033. struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
  7034. if (rcvd->max_resp_sz > sent->max_resp_sz)
  7035. return -EINVAL;
  7036. /*
  7037. * Our requested max_ops is the minimum we need; we're not
  7038. * prepared to break up compounds into smaller pieces than that.
  7039. * So, no point even trying to continue if the server won't
  7040. * cooperate:
  7041. */
  7042. if (rcvd->max_ops < sent->max_ops)
  7043. return -EINVAL;
  7044. if (rcvd->max_reqs == 0)
  7045. return -EINVAL;
  7046. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  7047. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  7048. return 0;
  7049. }
  7050. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
  7051. struct nfs41_create_session_res *res)
  7052. {
  7053. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  7054. struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
  7055. if (!(res->flags & SESSION4_BACK_CHAN))
  7056. goto out;
  7057. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  7058. return -EINVAL;
  7059. if (rcvd->max_resp_sz < sent->max_resp_sz)
  7060. return -EINVAL;
  7061. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  7062. return -EINVAL;
  7063. if (rcvd->max_ops > sent->max_ops)
  7064. return -EINVAL;
  7065. if (rcvd->max_reqs > sent->max_reqs)
  7066. return -EINVAL;
  7067. out:
  7068. return 0;
  7069. }
  7070. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  7071. struct nfs41_create_session_res *res)
  7072. {
  7073. int ret;
  7074. ret = nfs4_verify_fore_channel_attrs(args, res);
  7075. if (ret)
  7076. return ret;
  7077. return nfs4_verify_back_channel_attrs(args, res);
  7078. }
  7079. static void nfs4_update_session(struct nfs4_session *session,
  7080. struct nfs41_create_session_res *res)
  7081. {
  7082. nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
  7083. /* Mark client id and session as being confirmed */
  7084. session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  7085. set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
  7086. session->flags = res->flags;
  7087. memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
  7088. if (res->flags & SESSION4_BACK_CHAN)
  7089. memcpy(&session->bc_attrs, &res->bc_attrs,
  7090. sizeof(session->bc_attrs));
  7091. }
  7092. static int _nfs4_proc_create_session(struct nfs_client *clp,
  7093. struct rpc_cred *cred)
  7094. {
  7095. struct nfs4_session *session = clp->cl_session;
  7096. struct nfs41_create_session_args args = {
  7097. .client = clp,
  7098. .clientid = clp->cl_clientid,
  7099. .seqid = clp->cl_seqid,
  7100. .cb_program = NFS4_CALLBACK,
  7101. };
  7102. struct nfs41_create_session_res res;
  7103. struct rpc_message msg = {
  7104. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  7105. .rpc_argp = &args,
  7106. .rpc_resp = &res,
  7107. .rpc_cred = cred,
  7108. };
  7109. int status;
  7110. nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
  7111. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  7112. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7113. trace_nfs4_create_session(clp, status);
  7114. switch (status) {
  7115. case -NFS4ERR_STALE_CLIENTID:
  7116. case -NFS4ERR_DELAY:
  7117. case -ETIMEDOUT:
  7118. case -EACCES:
  7119. case -EAGAIN:
  7120. goto out;
  7121. };
  7122. clp->cl_seqid++;
  7123. if (!status) {
  7124. /* Verify the session's negotiated channel_attrs values */
  7125. status = nfs4_verify_channel_attrs(&args, &res);
  7126. /* Increment the clientid slot sequence id */
  7127. if (status)
  7128. goto out;
  7129. nfs4_update_session(session, &res);
  7130. }
  7131. out:
  7132. return status;
  7133. }
  7134. /*
  7135. * Issues a CREATE_SESSION operation to the server.
  7136. * It is the responsibility of the caller to verify the session is
  7137. * expired before calling this routine.
  7138. */
  7139. int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
  7140. {
  7141. int status;
  7142. unsigned *ptr;
  7143. struct nfs4_session *session = clp->cl_session;
  7144. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  7145. status = _nfs4_proc_create_session(clp, cred);
  7146. if (status)
  7147. goto out;
  7148. /* Init or reset the session slot tables */
  7149. status = nfs4_setup_session_slot_tables(session);
  7150. dprintk("slot table setup returned %d\n", status);
  7151. if (status)
  7152. goto out;
  7153. ptr = (unsigned *)&session->sess_id.data[0];
  7154. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  7155. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  7156. out:
  7157. dprintk("<-- %s\n", __func__);
  7158. return status;
  7159. }
  7160. /*
  7161. * Issue the over-the-wire RPC DESTROY_SESSION.
  7162. * The caller must serialize access to this routine.
  7163. */
  7164. int nfs4_proc_destroy_session(struct nfs4_session *session,
  7165. struct rpc_cred *cred)
  7166. {
  7167. struct rpc_message msg = {
  7168. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  7169. .rpc_argp = session,
  7170. .rpc_cred = cred,
  7171. };
  7172. int status = 0;
  7173. dprintk("--> nfs4_proc_destroy_session\n");
  7174. /* session is still being setup */
  7175. if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
  7176. return 0;
  7177. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7178. trace_nfs4_destroy_session(session->clp, status);
  7179. if (status)
  7180. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  7181. "Session has been destroyed regardless...\n", status);
  7182. dprintk("<-- nfs4_proc_destroy_session\n");
  7183. return status;
  7184. }
  7185. /*
  7186. * Renew the cl_session lease.
  7187. */
  7188. struct nfs4_sequence_data {
  7189. struct nfs_client *clp;
  7190. struct nfs4_sequence_args args;
  7191. struct nfs4_sequence_res res;
  7192. };
  7193. static void nfs41_sequence_release(void *data)
  7194. {
  7195. struct nfs4_sequence_data *calldata = data;
  7196. struct nfs_client *clp = calldata->clp;
  7197. if (atomic_read(&clp->cl_count) > 1)
  7198. nfs4_schedule_state_renewal(clp);
  7199. nfs_put_client(clp);
  7200. kfree(calldata);
  7201. }
  7202. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  7203. {
  7204. switch(task->tk_status) {
  7205. case -NFS4ERR_DELAY:
  7206. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  7207. return -EAGAIN;
  7208. default:
  7209. nfs4_schedule_lease_recovery(clp);
  7210. }
  7211. return 0;
  7212. }
  7213. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  7214. {
  7215. struct nfs4_sequence_data *calldata = data;
  7216. struct nfs_client *clp = calldata->clp;
  7217. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  7218. return;
  7219. trace_nfs4_sequence(clp, task->tk_status);
  7220. if (task->tk_status < 0) {
  7221. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  7222. if (atomic_read(&clp->cl_count) == 1)
  7223. goto out;
  7224. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  7225. rpc_restart_call_prepare(task);
  7226. return;
  7227. }
  7228. }
  7229. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  7230. out:
  7231. dprintk("<-- %s\n", __func__);
  7232. }
  7233. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  7234. {
  7235. struct nfs4_sequence_data *calldata = data;
  7236. struct nfs_client *clp = calldata->clp;
  7237. struct nfs4_sequence_args *args;
  7238. struct nfs4_sequence_res *res;
  7239. args = task->tk_msg.rpc_argp;
  7240. res = task->tk_msg.rpc_resp;
  7241. nfs4_setup_sequence(clp, args, res, task);
  7242. }
  7243. static const struct rpc_call_ops nfs41_sequence_ops = {
  7244. .rpc_call_done = nfs41_sequence_call_done,
  7245. .rpc_call_prepare = nfs41_sequence_prepare,
  7246. .rpc_release = nfs41_sequence_release,
  7247. };
  7248. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  7249. struct rpc_cred *cred,
  7250. bool is_privileged)
  7251. {
  7252. struct nfs4_sequence_data *calldata;
  7253. struct rpc_message msg = {
  7254. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  7255. .rpc_cred = cred,
  7256. };
  7257. struct rpc_task_setup task_setup_data = {
  7258. .rpc_client = clp->cl_rpcclient,
  7259. .rpc_message = &msg,
  7260. .callback_ops = &nfs41_sequence_ops,
  7261. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
  7262. };
  7263. if (!atomic_inc_not_zero(&clp->cl_count))
  7264. return ERR_PTR(-EIO);
  7265. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  7266. if (calldata == NULL) {
  7267. nfs_put_client(clp);
  7268. return ERR_PTR(-ENOMEM);
  7269. }
  7270. nfs4_init_sequence(&calldata->args, &calldata->res, 0);
  7271. if (is_privileged)
  7272. nfs4_set_sequence_privileged(&calldata->args);
  7273. msg.rpc_argp = &calldata->args;
  7274. msg.rpc_resp = &calldata->res;
  7275. calldata->clp = clp;
  7276. task_setup_data.callback_data = calldata;
  7277. return rpc_run_task(&task_setup_data);
  7278. }
  7279. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  7280. {
  7281. struct rpc_task *task;
  7282. int ret = 0;
  7283. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  7284. return -EAGAIN;
  7285. task = _nfs41_proc_sequence(clp, cred, false);
  7286. if (IS_ERR(task))
  7287. ret = PTR_ERR(task);
  7288. else
  7289. rpc_put_task_async(task);
  7290. dprintk("<-- %s status=%d\n", __func__, ret);
  7291. return ret;
  7292. }
  7293. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  7294. {
  7295. struct rpc_task *task;
  7296. int ret;
  7297. task = _nfs41_proc_sequence(clp, cred, true);
  7298. if (IS_ERR(task)) {
  7299. ret = PTR_ERR(task);
  7300. goto out;
  7301. }
  7302. ret = rpc_wait_for_completion_task(task);
  7303. if (!ret)
  7304. ret = task->tk_status;
  7305. rpc_put_task(task);
  7306. out:
  7307. dprintk("<-- %s status=%d\n", __func__, ret);
  7308. return ret;
  7309. }
  7310. struct nfs4_reclaim_complete_data {
  7311. struct nfs_client *clp;
  7312. struct nfs41_reclaim_complete_args arg;
  7313. struct nfs41_reclaim_complete_res res;
  7314. };
  7315. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  7316. {
  7317. struct nfs4_reclaim_complete_data *calldata = data;
  7318. nfs4_setup_sequence(calldata->clp,
  7319. &calldata->arg.seq_args,
  7320. &calldata->res.seq_res,
  7321. task);
  7322. }
  7323. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  7324. {
  7325. switch(task->tk_status) {
  7326. case 0:
  7327. case -NFS4ERR_COMPLETE_ALREADY:
  7328. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  7329. break;
  7330. case -NFS4ERR_DELAY:
  7331. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  7332. /* fall through */
  7333. case -NFS4ERR_RETRY_UNCACHED_REP:
  7334. return -EAGAIN;
  7335. case -NFS4ERR_BADSESSION:
  7336. case -NFS4ERR_DEADSESSION:
  7337. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  7338. nfs4_schedule_session_recovery(clp->cl_session,
  7339. task->tk_status);
  7340. break;
  7341. default:
  7342. nfs4_schedule_lease_recovery(clp);
  7343. }
  7344. return 0;
  7345. }
  7346. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  7347. {
  7348. struct nfs4_reclaim_complete_data *calldata = data;
  7349. struct nfs_client *clp = calldata->clp;
  7350. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  7351. dprintk("--> %s\n", __func__);
  7352. if (!nfs41_sequence_done(task, res))
  7353. return;
  7354. trace_nfs4_reclaim_complete(clp, task->tk_status);
  7355. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  7356. rpc_restart_call_prepare(task);
  7357. return;
  7358. }
  7359. dprintk("<-- %s\n", __func__);
  7360. }
  7361. static void nfs4_free_reclaim_complete_data(void *data)
  7362. {
  7363. struct nfs4_reclaim_complete_data *calldata = data;
  7364. kfree(calldata);
  7365. }
  7366. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  7367. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  7368. .rpc_call_done = nfs4_reclaim_complete_done,
  7369. .rpc_release = nfs4_free_reclaim_complete_data,
  7370. };
  7371. /*
  7372. * Issue a global reclaim complete.
  7373. */
  7374. static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
  7375. struct rpc_cred *cred)
  7376. {
  7377. struct nfs4_reclaim_complete_data *calldata;
  7378. struct rpc_task *task;
  7379. struct rpc_message msg = {
  7380. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  7381. .rpc_cred = cred,
  7382. };
  7383. struct rpc_task_setup task_setup_data = {
  7384. .rpc_client = clp->cl_rpcclient,
  7385. .rpc_message = &msg,
  7386. .callback_ops = &nfs4_reclaim_complete_call_ops,
  7387. .flags = RPC_TASK_ASYNC,
  7388. };
  7389. int status = -ENOMEM;
  7390. dprintk("--> %s\n", __func__);
  7391. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  7392. if (calldata == NULL)
  7393. goto out;
  7394. calldata->clp = clp;
  7395. calldata->arg.one_fs = 0;
  7396. nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
  7397. nfs4_set_sequence_privileged(&calldata->arg.seq_args);
  7398. msg.rpc_argp = &calldata->arg;
  7399. msg.rpc_resp = &calldata->res;
  7400. task_setup_data.callback_data = calldata;
  7401. task = rpc_run_task(&task_setup_data);
  7402. if (IS_ERR(task)) {
  7403. status = PTR_ERR(task);
  7404. goto out;
  7405. }
  7406. status = rpc_wait_for_completion_task(task);
  7407. if (status == 0)
  7408. status = task->tk_status;
  7409. rpc_put_task(task);
  7410. out:
  7411. dprintk("<-- %s status=%d\n", __func__, status);
  7412. return status;
  7413. }
  7414. static void
  7415. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  7416. {
  7417. struct nfs4_layoutget *lgp = calldata;
  7418. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  7419. dprintk("--> %s\n", __func__);
  7420. nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
  7421. &lgp->res.seq_res, task);
  7422. dprintk("<-- %s\n", __func__);
  7423. }
  7424. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  7425. {
  7426. struct nfs4_layoutget *lgp = calldata;
  7427. dprintk("--> %s\n", __func__);
  7428. nfs41_sequence_process(task, &lgp->res.seq_res);
  7429. dprintk("<-- %s\n", __func__);
  7430. }
  7431. static int
  7432. nfs4_layoutget_handle_exception(struct rpc_task *task,
  7433. struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
  7434. {
  7435. struct inode *inode = lgp->args.inode;
  7436. struct nfs_server *server = NFS_SERVER(inode);
  7437. struct pnfs_layout_hdr *lo;
  7438. int nfs4err = task->tk_status;
  7439. int err, status = 0;
  7440. LIST_HEAD(head);
  7441. dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
  7442. switch (nfs4err) {
  7443. case 0:
  7444. goto out;
  7445. /*
  7446. * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
  7447. * on the file. set tk_status to -ENODATA to tell upper layer to
  7448. * retry go inband.
  7449. */
  7450. case -NFS4ERR_LAYOUTUNAVAILABLE:
  7451. status = -ENODATA;
  7452. goto out;
  7453. /*
  7454. * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
  7455. * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
  7456. */
  7457. case -NFS4ERR_BADLAYOUT:
  7458. status = -EOVERFLOW;
  7459. goto out;
  7460. /*
  7461. * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
  7462. * (or clients) writing to the same RAID stripe except when
  7463. * the minlength argument is 0 (see RFC5661 section 18.43.3).
  7464. *
  7465. * Treat it like we would RECALLCONFLICT -- we retry for a little
  7466. * while, and then eventually give up.
  7467. */
  7468. case -NFS4ERR_LAYOUTTRYLATER:
  7469. if (lgp->args.minlength == 0) {
  7470. status = -EOVERFLOW;
  7471. goto out;
  7472. }
  7473. status = -EBUSY;
  7474. break;
  7475. case -NFS4ERR_RECALLCONFLICT:
  7476. status = -ERECALLCONFLICT;
  7477. break;
  7478. case -NFS4ERR_DELEG_REVOKED:
  7479. case -NFS4ERR_ADMIN_REVOKED:
  7480. case -NFS4ERR_EXPIRED:
  7481. case -NFS4ERR_BAD_STATEID:
  7482. exception->timeout = 0;
  7483. spin_lock(&inode->i_lock);
  7484. lo = NFS_I(inode)->layout;
  7485. /* If the open stateid was bad, then recover it. */
  7486. if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
  7487. nfs4_stateid_match_other(&lgp->args.stateid,
  7488. &lgp->args.ctx->state->stateid)) {
  7489. spin_unlock(&inode->i_lock);
  7490. exception->state = lgp->args.ctx->state;
  7491. exception->stateid = &lgp->args.stateid;
  7492. break;
  7493. }
  7494. /*
  7495. * Mark the bad layout state as invalid, then retry
  7496. */
  7497. pnfs_mark_layout_stateid_invalid(lo, &head);
  7498. spin_unlock(&inode->i_lock);
  7499. nfs_commit_inode(inode, 0);
  7500. pnfs_free_lseg_list(&head);
  7501. status = -EAGAIN;
  7502. goto out;
  7503. }
  7504. nfs4_sequence_free_slot(&lgp->res.seq_res);
  7505. err = nfs4_handle_exception(server, nfs4err, exception);
  7506. if (!status) {
  7507. if (exception->retry)
  7508. status = -EAGAIN;
  7509. else
  7510. status = err;
  7511. }
  7512. out:
  7513. dprintk("<-- %s\n", __func__);
  7514. return status;
  7515. }
  7516. static size_t max_response_pages(struct nfs_server *server)
  7517. {
  7518. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  7519. return nfs_page_array_len(0, max_resp_sz);
  7520. }
  7521. static void nfs4_free_pages(struct page **pages, size_t size)
  7522. {
  7523. int i;
  7524. if (!pages)
  7525. return;
  7526. for (i = 0; i < size; i++) {
  7527. if (!pages[i])
  7528. break;
  7529. __free_page(pages[i]);
  7530. }
  7531. kfree(pages);
  7532. }
  7533. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  7534. {
  7535. struct page **pages;
  7536. int i;
  7537. pages = kcalloc(size, sizeof(struct page *), gfp_flags);
  7538. if (!pages) {
  7539. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  7540. return NULL;
  7541. }
  7542. for (i = 0; i < size; i++) {
  7543. pages[i] = alloc_page(gfp_flags);
  7544. if (!pages[i]) {
  7545. dprintk("%s: failed to allocate page\n", __func__);
  7546. nfs4_free_pages(pages, size);
  7547. return NULL;
  7548. }
  7549. }
  7550. return pages;
  7551. }
  7552. static void nfs4_layoutget_release(void *calldata)
  7553. {
  7554. struct nfs4_layoutget *lgp = calldata;
  7555. struct inode *inode = lgp->args.inode;
  7556. struct nfs_server *server = NFS_SERVER(inode);
  7557. size_t max_pages = max_response_pages(server);
  7558. dprintk("--> %s\n", __func__);
  7559. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  7560. pnfs_put_layout_hdr(NFS_I(inode)->layout);
  7561. put_nfs_open_context(lgp->args.ctx);
  7562. kfree(calldata);
  7563. dprintk("<-- %s\n", __func__);
  7564. }
  7565. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  7566. .rpc_call_prepare = nfs4_layoutget_prepare,
  7567. .rpc_call_done = nfs4_layoutget_done,
  7568. .rpc_release = nfs4_layoutget_release,
  7569. };
  7570. struct pnfs_layout_segment *
  7571. nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
  7572. {
  7573. struct inode *inode = lgp->args.inode;
  7574. struct nfs_server *server = NFS_SERVER(inode);
  7575. size_t max_pages = max_response_pages(server);
  7576. struct rpc_task *task;
  7577. struct rpc_message msg = {
  7578. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  7579. .rpc_argp = &lgp->args,
  7580. .rpc_resp = &lgp->res,
  7581. .rpc_cred = lgp->cred,
  7582. };
  7583. struct rpc_task_setup task_setup_data = {
  7584. .rpc_client = server->client,
  7585. .rpc_message = &msg,
  7586. .callback_ops = &nfs4_layoutget_call_ops,
  7587. .callback_data = lgp,
  7588. .flags = RPC_TASK_ASYNC,
  7589. };
  7590. struct pnfs_layout_segment *lseg = NULL;
  7591. struct nfs4_exception exception = {
  7592. .inode = inode,
  7593. .timeout = *timeout,
  7594. };
  7595. int status = 0;
  7596. dprintk("--> %s\n", __func__);
  7597. /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
  7598. pnfs_get_layout_hdr(NFS_I(inode)->layout);
  7599. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  7600. if (!lgp->args.layout.pages) {
  7601. nfs4_layoutget_release(lgp);
  7602. return ERR_PTR(-ENOMEM);
  7603. }
  7604. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  7605. lgp->res.layoutp = &lgp->args.layout;
  7606. lgp->res.seq_res.sr_slot = NULL;
  7607. nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
  7608. task = rpc_run_task(&task_setup_data);
  7609. if (IS_ERR(task))
  7610. return ERR_CAST(task);
  7611. status = rpc_wait_for_completion_task(task);
  7612. if (status == 0) {
  7613. status = nfs4_layoutget_handle_exception(task, lgp, &exception);
  7614. *timeout = exception.timeout;
  7615. }
  7616. trace_nfs4_layoutget(lgp->args.ctx,
  7617. &lgp->args.range,
  7618. &lgp->res.range,
  7619. &lgp->res.stateid,
  7620. status);
  7621. /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
  7622. if (status == 0 && lgp->res.layoutp->len)
  7623. lseg = pnfs_layout_process(lgp);
  7624. nfs4_sequence_free_slot(&lgp->res.seq_res);
  7625. rpc_put_task(task);
  7626. dprintk("<-- %s status=%d\n", __func__, status);
  7627. if (status)
  7628. return ERR_PTR(status);
  7629. return lseg;
  7630. }
  7631. static void
  7632. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  7633. {
  7634. struct nfs4_layoutreturn *lrp = calldata;
  7635. dprintk("--> %s\n", __func__);
  7636. nfs4_setup_sequence(lrp->clp,
  7637. &lrp->args.seq_args,
  7638. &lrp->res.seq_res,
  7639. task);
  7640. }
  7641. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  7642. {
  7643. struct nfs4_layoutreturn *lrp = calldata;
  7644. struct nfs_server *server;
  7645. dprintk("--> %s\n", __func__);
  7646. if (!nfs41_sequence_process(task, &lrp->res.seq_res))
  7647. return;
  7648. server = NFS_SERVER(lrp->args.inode);
  7649. switch (task->tk_status) {
  7650. default:
  7651. task->tk_status = 0;
  7652. case 0:
  7653. break;
  7654. case -NFS4ERR_DELAY:
  7655. if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
  7656. break;
  7657. nfs4_sequence_free_slot(&lrp->res.seq_res);
  7658. rpc_restart_call_prepare(task);
  7659. return;
  7660. }
  7661. dprintk("<-- %s\n", __func__);
  7662. }
  7663. static void nfs4_layoutreturn_release(void *calldata)
  7664. {
  7665. struct nfs4_layoutreturn *lrp = calldata;
  7666. struct pnfs_layout_hdr *lo = lrp->args.layout;
  7667. dprintk("--> %s\n", __func__);
  7668. pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
  7669. lrp->res.lrs_present ? &lrp->res.stateid : NULL);
  7670. nfs4_sequence_free_slot(&lrp->res.seq_res);
  7671. if (lrp->ld_private.ops && lrp->ld_private.ops->free)
  7672. lrp->ld_private.ops->free(&lrp->ld_private);
  7673. pnfs_put_layout_hdr(lrp->args.layout);
  7674. nfs_iput_and_deactive(lrp->inode);
  7675. kfree(calldata);
  7676. dprintk("<-- %s\n", __func__);
  7677. }
  7678. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  7679. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  7680. .rpc_call_done = nfs4_layoutreturn_done,
  7681. .rpc_release = nfs4_layoutreturn_release,
  7682. };
  7683. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
  7684. {
  7685. struct rpc_task *task;
  7686. struct rpc_message msg = {
  7687. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  7688. .rpc_argp = &lrp->args,
  7689. .rpc_resp = &lrp->res,
  7690. .rpc_cred = lrp->cred,
  7691. };
  7692. struct rpc_task_setup task_setup_data = {
  7693. .rpc_client = NFS_SERVER(lrp->args.inode)->client,
  7694. .rpc_message = &msg,
  7695. .callback_ops = &nfs4_layoutreturn_call_ops,
  7696. .callback_data = lrp,
  7697. };
  7698. int status = 0;
  7699. nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
  7700. NFS_SP4_MACH_CRED_PNFS_CLEANUP,
  7701. &task_setup_data.rpc_client, &msg);
  7702. dprintk("--> %s\n", __func__);
  7703. if (!sync) {
  7704. lrp->inode = nfs_igrab_and_active(lrp->args.inode);
  7705. if (!lrp->inode) {
  7706. nfs4_layoutreturn_release(lrp);
  7707. return -EAGAIN;
  7708. }
  7709. task_setup_data.flags |= RPC_TASK_ASYNC;
  7710. }
  7711. nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
  7712. task = rpc_run_task(&task_setup_data);
  7713. if (IS_ERR(task))
  7714. return PTR_ERR(task);
  7715. if (sync)
  7716. status = task->tk_status;
  7717. trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
  7718. dprintk("<-- %s status=%d\n", __func__, status);
  7719. rpc_put_task(task);
  7720. return status;
  7721. }
  7722. static int
  7723. _nfs4_proc_getdeviceinfo(struct nfs_server *server,
  7724. struct pnfs_device *pdev,
  7725. struct rpc_cred *cred)
  7726. {
  7727. struct nfs4_getdeviceinfo_args args = {
  7728. .pdev = pdev,
  7729. .notify_types = NOTIFY_DEVICEID4_CHANGE |
  7730. NOTIFY_DEVICEID4_DELETE,
  7731. };
  7732. struct nfs4_getdeviceinfo_res res = {
  7733. .pdev = pdev,
  7734. };
  7735. struct rpc_message msg = {
  7736. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  7737. .rpc_argp = &args,
  7738. .rpc_resp = &res,
  7739. .rpc_cred = cred,
  7740. };
  7741. int status;
  7742. dprintk("--> %s\n", __func__);
  7743. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  7744. if (res.notification & ~args.notify_types)
  7745. dprintk("%s: unsupported notification\n", __func__);
  7746. if (res.notification != args.notify_types)
  7747. pdev->nocache = 1;
  7748. dprintk("<-- %s status=%d\n", __func__, status);
  7749. return status;
  7750. }
  7751. int nfs4_proc_getdeviceinfo(struct nfs_server *server,
  7752. struct pnfs_device *pdev,
  7753. struct rpc_cred *cred)
  7754. {
  7755. struct nfs4_exception exception = { };
  7756. int err;
  7757. do {
  7758. err = nfs4_handle_exception(server,
  7759. _nfs4_proc_getdeviceinfo(server, pdev, cred),
  7760. &exception);
  7761. } while (exception.retry);
  7762. return err;
  7763. }
  7764. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  7765. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  7766. {
  7767. struct nfs4_layoutcommit_data *data = calldata;
  7768. struct nfs_server *server = NFS_SERVER(data->args.inode);
  7769. nfs4_setup_sequence(server->nfs_client,
  7770. &data->args.seq_args,
  7771. &data->res.seq_res,
  7772. task);
  7773. }
  7774. static void
  7775. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  7776. {
  7777. struct nfs4_layoutcommit_data *data = calldata;
  7778. struct nfs_server *server = NFS_SERVER(data->args.inode);
  7779. if (!nfs41_sequence_done(task, &data->res.seq_res))
  7780. return;
  7781. switch (task->tk_status) { /* Just ignore these failures */
  7782. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  7783. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  7784. case -NFS4ERR_BADLAYOUT: /* no layout */
  7785. case -NFS4ERR_GRACE: /* loca_recalim always false */
  7786. task->tk_status = 0;
  7787. case 0:
  7788. break;
  7789. default:
  7790. if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
  7791. rpc_restart_call_prepare(task);
  7792. return;
  7793. }
  7794. }
  7795. }
  7796. static void nfs4_layoutcommit_release(void *calldata)
  7797. {
  7798. struct nfs4_layoutcommit_data *data = calldata;
  7799. pnfs_cleanup_layoutcommit(data);
  7800. nfs_post_op_update_inode_force_wcc(data->args.inode,
  7801. data->res.fattr);
  7802. put_rpccred(data->cred);
  7803. nfs_iput_and_deactive(data->inode);
  7804. kfree(data);
  7805. }
  7806. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  7807. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  7808. .rpc_call_done = nfs4_layoutcommit_done,
  7809. .rpc_release = nfs4_layoutcommit_release,
  7810. };
  7811. int
  7812. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  7813. {
  7814. struct rpc_message msg = {
  7815. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  7816. .rpc_argp = &data->args,
  7817. .rpc_resp = &data->res,
  7818. .rpc_cred = data->cred,
  7819. };
  7820. struct rpc_task_setup task_setup_data = {
  7821. .task = &data->task,
  7822. .rpc_client = NFS_CLIENT(data->args.inode),
  7823. .rpc_message = &msg,
  7824. .callback_ops = &nfs4_layoutcommit_ops,
  7825. .callback_data = data,
  7826. };
  7827. struct rpc_task *task;
  7828. int status = 0;
  7829. dprintk("NFS: initiating layoutcommit call. sync %d "
  7830. "lbw: %llu inode %lu\n", sync,
  7831. data->args.lastbytewritten,
  7832. data->args.inode->i_ino);
  7833. if (!sync) {
  7834. data->inode = nfs_igrab_and_active(data->args.inode);
  7835. if (data->inode == NULL) {
  7836. nfs4_layoutcommit_release(data);
  7837. return -EAGAIN;
  7838. }
  7839. task_setup_data.flags = RPC_TASK_ASYNC;
  7840. }
  7841. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  7842. task = rpc_run_task(&task_setup_data);
  7843. if (IS_ERR(task))
  7844. return PTR_ERR(task);
  7845. if (sync)
  7846. status = task->tk_status;
  7847. trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
  7848. dprintk("%s: status %d\n", __func__, status);
  7849. rpc_put_task(task);
  7850. return status;
  7851. }
  7852. /**
  7853. * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
  7854. * possible) as per RFC3530bis and RFC5661 Security Considerations sections
  7855. */
  7856. static int
  7857. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  7858. struct nfs_fsinfo *info,
  7859. struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  7860. {
  7861. struct nfs41_secinfo_no_name_args args = {
  7862. .style = SECINFO_STYLE_CURRENT_FH,
  7863. };
  7864. struct nfs4_secinfo_res res = {
  7865. .flavors = flavors,
  7866. };
  7867. struct rpc_message msg = {
  7868. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  7869. .rpc_argp = &args,
  7870. .rpc_resp = &res,
  7871. };
  7872. struct rpc_clnt *clnt = server->client;
  7873. struct rpc_cred *cred = NULL;
  7874. int status;
  7875. if (use_integrity) {
  7876. clnt = server->nfs_client->cl_rpcclient;
  7877. cred = nfs4_get_clid_cred(server->nfs_client);
  7878. msg.rpc_cred = cred;
  7879. }
  7880. dprintk("--> %s\n", __func__);
  7881. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
  7882. &res.seq_res, 0);
  7883. dprintk("<-- %s status=%d\n", __func__, status);
  7884. if (cred)
  7885. put_rpccred(cred);
  7886. return status;
  7887. }
  7888. static int
  7889. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  7890. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  7891. {
  7892. struct nfs4_exception exception = { };
  7893. int err;
  7894. do {
  7895. /* first try using integrity protection */
  7896. err = -NFS4ERR_WRONGSEC;
  7897. /* try to use integrity protection with machine cred */
  7898. if (_nfs4_is_integrity_protected(server->nfs_client))
  7899. err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
  7900. flavors, true);
  7901. /*
  7902. * if unable to use integrity protection, or SECINFO with
  7903. * integrity protection returns NFS4ERR_WRONGSEC (which is
  7904. * disallowed by spec, but exists in deployed servers) use
  7905. * the current filesystem's rpc_client and the user cred.
  7906. */
  7907. if (err == -NFS4ERR_WRONGSEC)
  7908. err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
  7909. flavors, false);
  7910. switch (err) {
  7911. case 0:
  7912. case -NFS4ERR_WRONGSEC:
  7913. case -ENOTSUPP:
  7914. goto out;
  7915. default:
  7916. err = nfs4_handle_exception(server, err, &exception);
  7917. }
  7918. } while (exception.retry);
  7919. out:
  7920. return err;
  7921. }
  7922. static int
  7923. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  7924. struct nfs_fsinfo *info)
  7925. {
  7926. int err;
  7927. struct page *page;
  7928. rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
  7929. struct nfs4_secinfo_flavors *flavors;
  7930. struct nfs4_secinfo4 *secinfo;
  7931. int i;
  7932. page = alloc_page(GFP_KERNEL);
  7933. if (!page) {
  7934. err = -ENOMEM;
  7935. goto out;
  7936. }
  7937. flavors = page_address(page);
  7938. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  7939. /*
  7940. * Fall back on "guess and check" method if
  7941. * the server doesn't support SECINFO_NO_NAME
  7942. */
  7943. if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
  7944. err = nfs4_find_root_sec(server, fhandle, info);
  7945. goto out_freepage;
  7946. }
  7947. if (err)
  7948. goto out_freepage;
  7949. for (i = 0; i < flavors->num_flavors; i++) {
  7950. secinfo = &flavors->flavors[i];
  7951. switch (secinfo->flavor) {
  7952. case RPC_AUTH_NULL:
  7953. case RPC_AUTH_UNIX:
  7954. case RPC_AUTH_GSS:
  7955. flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  7956. &secinfo->flavor_info);
  7957. break;
  7958. default:
  7959. flavor = RPC_AUTH_MAXFLAVOR;
  7960. break;
  7961. }
  7962. if (!nfs_auth_info_match(&server->auth_info, flavor))
  7963. flavor = RPC_AUTH_MAXFLAVOR;
  7964. if (flavor != RPC_AUTH_MAXFLAVOR) {
  7965. err = nfs4_lookup_root_sec(server, fhandle,
  7966. info, flavor);
  7967. if (!err)
  7968. break;
  7969. }
  7970. }
  7971. if (flavor == RPC_AUTH_MAXFLAVOR)
  7972. err = -EPERM;
  7973. out_freepage:
  7974. put_page(page);
  7975. if (err == -EACCES)
  7976. return -EPERM;
  7977. out:
  7978. return err;
  7979. }
  7980. static int _nfs41_test_stateid(struct nfs_server *server,
  7981. nfs4_stateid *stateid,
  7982. struct rpc_cred *cred)
  7983. {
  7984. int status;
  7985. struct nfs41_test_stateid_args args = {
  7986. .stateid = stateid,
  7987. };
  7988. struct nfs41_test_stateid_res res;
  7989. struct rpc_message msg = {
  7990. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  7991. .rpc_argp = &args,
  7992. .rpc_resp = &res,
  7993. .rpc_cred = cred,
  7994. };
  7995. struct rpc_clnt *rpc_client = server->client;
  7996. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
  7997. &rpc_client, &msg);
  7998. dprintk("NFS call test_stateid %p\n", stateid);
  7999. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  8000. nfs4_set_sequence_privileged(&args.seq_args);
  8001. status = nfs4_call_sync_sequence(rpc_client, server, &msg,
  8002. &args.seq_args, &res.seq_res);
  8003. if (status != NFS_OK) {
  8004. dprintk("NFS reply test_stateid: failed, %d\n", status);
  8005. return status;
  8006. }
  8007. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  8008. return -res.status;
  8009. }
  8010. static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
  8011. int err, struct nfs4_exception *exception)
  8012. {
  8013. exception->retry = 0;
  8014. switch(err) {
  8015. case -NFS4ERR_DELAY:
  8016. case -NFS4ERR_RETRY_UNCACHED_REP:
  8017. nfs4_handle_exception(server, err, exception);
  8018. break;
  8019. case -NFS4ERR_BADSESSION:
  8020. case -NFS4ERR_BADSLOT:
  8021. case -NFS4ERR_BAD_HIGH_SLOT:
  8022. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  8023. case -NFS4ERR_DEADSESSION:
  8024. nfs4_do_handle_exception(server, err, exception);
  8025. }
  8026. }
  8027. /**
  8028. * nfs41_test_stateid - perform a TEST_STATEID operation
  8029. *
  8030. * @server: server / transport on which to perform the operation
  8031. * @stateid: state ID to test
  8032. * @cred: credential
  8033. *
  8034. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  8035. * Otherwise a negative NFS4ERR value is returned if the operation
  8036. * failed or the state ID is not currently valid.
  8037. */
  8038. static int nfs41_test_stateid(struct nfs_server *server,
  8039. nfs4_stateid *stateid,
  8040. struct rpc_cred *cred)
  8041. {
  8042. struct nfs4_exception exception = { };
  8043. int err;
  8044. do {
  8045. err = _nfs41_test_stateid(server, stateid, cred);
  8046. nfs4_handle_delay_or_session_error(server, err, &exception);
  8047. } while (exception.retry);
  8048. return err;
  8049. }
  8050. struct nfs_free_stateid_data {
  8051. struct nfs_server *server;
  8052. struct nfs41_free_stateid_args args;
  8053. struct nfs41_free_stateid_res res;
  8054. };
  8055. static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
  8056. {
  8057. struct nfs_free_stateid_data *data = calldata;
  8058. nfs4_setup_sequence(data->server->nfs_client,
  8059. &data->args.seq_args,
  8060. &data->res.seq_res,
  8061. task);
  8062. }
  8063. static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
  8064. {
  8065. struct nfs_free_stateid_data *data = calldata;
  8066. nfs41_sequence_done(task, &data->res.seq_res);
  8067. switch (task->tk_status) {
  8068. case -NFS4ERR_DELAY:
  8069. if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
  8070. rpc_restart_call_prepare(task);
  8071. }
  8072. }
  8073. static void nfs41_free_stateid_release(void *calldata)
  8074. {
  8075. kfree(calldata);
  8076. }
  8077. static const struct rpc_call_ops nfs41_free_stateid_ops = {
  8078. .rpc_call_prepare = nfs41_free_stateid_prepare,
  8079. .rpc_call_done = nfs41_free_stateid_done,
  8080. .rpc_release = nfs41_free_stateid_release,
  8081. };
  8082. static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
  8083. const nfs4_stateid *stateid,
  8084. struct rpc_cred *cred,
  8085. bool privileged)
  8086. {
  8087. struct rpc_message msg = {
  8088. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  8089. .rpc_cred = cred,
  8090. };
  8091. struct rpc_task_setup task_setup = {
  8092. .rpc_client = server->client,
  8093. .rpc_message = &msg,
  8094. .callback_ops = &nfs41_free_stateid_ops,
  8095. .flags = RPC_TASK_ASYNC,
  8096. };
  8097. struct nfs_free_stateid_data *data;
  8098. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
  8099. &task_setup.rpc_client, &msg);
  8100. dprintk("NFS call free_stateid %p\n", stateid);
  8101. data = kmalloc(sizeof(*data), GFP_NOFS);
  8102. if (!data)
  8103. return ERR_PTR(-ENOMEM);
  8104. data->server = server;
  8105. nfs4_stateid_copy(&data->args.stateid, stateid);
  8106. task_setup.callback_data = data;
  8107. msg.rpc_argp = &data->args;
  8108. msg.rpc_resp = &data->res;
  8109. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  8110. if (privileged)
  8111. nfs4_set_sequence_privileged(&data->args.seq_args);
  8112. return rpc_run_task(&task_setup);
  8113. }
  8114. /**
  8115. * nfs41_free_stateid - perform a FREE_STATEID operation
  8116. *
  8117. * @server: server / transport on which to perform the operation
  8118. * @stateid: state ID to release
  8119. * @cred: credential
  8120. * @is_recovery: set to true if this call needs to be privileged
  8121. *
  8122. * Note: this function is always asynchronous.
  8123. */
  8124. static int nfs41_free_stateid(struct nfs_server *server,
  8125. const nfs4_stateid *stateid,
  8126. struct rpc_cred *cred,
  8127. bool is_recovery)
  8128. {
  8129. struct rpc_task *task;
  8130. task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
  8131. if (IS_ERR(task))
  8132. return PTR_ERR(task);
  8133. rpc_put_task(task);
  8134. return 0;
  8135. }
  8136. static void
  8137. nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  8138. {
  8139. struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
  8140. nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
  8141. nfs4_free_lock_state(server, lsp);
  8142. }
  8143. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  8144. const nfs4_stateid *s2)
  8145. {
  8146. if (s1->type != s2->type)
  8147. return false;
  8148. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  8149. return false;
  8150. if (s1->seqid == s2->seqid)
  8151. return true;
  8152. return s1->seqid == 0 || s2->seqid == 0;
  8153. }
  8154. #endif /* CONFIG_NFS_V4_1 */
  8155. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  8156. const nfs4_stateid *s2)
  8157. {
  8158. return nfs4_stateid_match(s1, s2);
  8159. }
  8160. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  8161. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  8162. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  8163. .recover_open = nfs4_open_reclaim,
  8164. .recover_lock = nfs4_lock_reclaim,
  8165. .establish_clid = nfs4_init_clientid,
  8166. .detect_trunking = nfs40_discover_server_trunking,
  8167. };
  8168. #if defined(CONFIG_NFS_V4_1)
  8169. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  8170. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  8171. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  8172. .recover_open = nfs4_open_reclaim,
  8173. .recover_lock = nfs4_lock_reclaim,
  8174. .establish_clid = nfs41_init_clientid,
  8175. .reclaim_complete = nfs41_proc_reclaim_complete,
  8176. .detect_trunking = nfs41_discover_server_trunking,
  8177. };
  8178. #endif /* CONFIG_NFS_V4_1 */
  8179. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  8180. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  8181. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  8182. .recover_open = nfs40_open_expired,
  8183. .recover_lock = nfs4_lock_expired,
  8184. .establish_clid = nfs4_init_clientid,
  8185. };
  8186. #if defined(CONFIG_NFS_V4_1)
  8187. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  8188. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  8189. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  8190. .recover_open = nfs41_open_expired,
  8191. .recover_lock = nfs41_lock_expired,
  8192. .establish_clid = nfs41_init_clientid,
  8193. };
  8194. #endif /* CONFIG_NFS_V4_1 */
  8195. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  8196. .sched_state_renewal = nfs4_proc_async_renew,
  8197. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  8198. .renew_lease = nfs4_proc_renew,
  8199. };
  8200. #if defined(CONFIG_NFS_V4_1)
  8201. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  8202. .sched_state_renewal = nfs41_proc_async_sequence,
  8203. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  8204. .renew_lease = nfs4_proc_sequence,
  8205. };
  8206. #endif
  8207. static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
  8208. .get_locations = _nfs40_proc_get_locations,
  8209. .fsid_present = _nfs40_proc_fsid_present,
  8210. };
  8211. #if defined(CONFIG_NFS_V4_1)
  8212. static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
  8213. .get_locations = _nfs41_proc_get_locations,
  8214. .fsid_present = _nfs41_proc_fsid_present,
  8215. };
  8216. #endif /* CONFIG_NFS_V4_1 */
  8217. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  8218. .minor_version = 0,
  8219. .init_caps = NFS_CAP_READDIRPLUS
  8220. | NFS_CAP_ATOMIC_OPEN
  8221. | NFS_CAP_POSIX_LOCK,
  8222. .init_client = nfs40_init_client,
  8223. .shutdown_client = nfs40_shutdown_client,
  8224. .match_stateid = nfs4_match_stateid,
  8225. .find_root_sec = nfs4_find_root_sec,
  8226. .free_lock_state = nfs4_release_lockowner,
  8227. .test_and_free_expired = nfs40_test_and_free_expired_stateid,
  8228. .alloc_seqid = nfs_alloc_seqid,
  8229. .call_sync_ops = &nfs40_call_sync_ops,
  8230. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  8231. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  8232. .state_renewal_ops = &nfs40_state_renewal_ops,
  8233. .mig_recovery_ops = &nfs40_mig_recovery_ops,
  8234. };
  8235. #if defined(CONFIG_NFS_V4_1)
  8236. static struct nfs_seqid *
  8237. nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
  8238. {
  8239. return NULL;
  8240. }
  8241. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  8242. .minor_version = 1,
  8243. .init_caps = NFS_CAP_READDIRPLUS
  8244. | NFS_CAP_ATOMIC_OPEN
  8245. | NFS_CAP_POSIX_LOCK
  8246. | NFS_CAP_STATEID_NFSV41
  8247. | NFS_CAP_ATOMIC_OPEN_V1,
  8248. .init_client = nfs41_init_client,
  8249. .shutdown_client = nfs41_shutdown_client,
  8250. .match_stateid = nfs41_match_stateid,
  8251. .find_root_sec = nfs41_find_root_sec,
  8252. .free_lock_state = nfs41_free_lock_state,
  8253. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  8254. .alloc_seqid = nfs_alloc_no_seqid,
  8255. .session_trunk = nfs4_test_session_trunk,
  8256. .call_sync_ops = &nfs41_call_sync_ops,
  8257. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  8258. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  8259. .state_renewal_ops = &nfs41_state_renewal_ops,
  8260. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  8261. };
  8262. #endif
  8263. #if defined(CONFIG_NFS_V4_2)
  8264. static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
  8265. .minor_version = 2,
  8266. .init_caps = NFS_CAP_READDIRPLUS
  8267. | NFS_CAP_ATOMIC_OPEN
  8268. | NFS_CAP_POSIX_LOCK
  8269. | NFS_CAP_STATEID_NFSV41
  8270. | NFS_CAP_ATOMIC_OPEN_V1
  8271. | NFS_CAP_ALLOCATE
  8272. | NFS_CAP_COPY
  8273. | NFS_CAP_DEALLOCATE
  8274. | NFS_CAP_SEEK
  8275. | NFS_CAP_LAYOUTSTATS
  8276. | NFS_CAP_CLONE,
  8277. .init_client = nfs41_init_client,
  8278. .shutdown_client = nfs41_shutdown_client,
  8279. .match_stateid = nfs41_match_stateid,
  8280. .find_root_sec = nfs41_find_root_sec,
  8281. .free_lock_state = nfs41_free_lock_state,
  8282. .call_sync_ops = &nfs41_call_sync_ops,
  8283. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  8284. .alloc_seqid = nfs_alloc_no_seqid,
  8285. .session_trunk = nfs4_test_session_trunk,
  8286. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  8287. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  8288. .state_renewal_ops = &nfs41_state_renewal_ops,
  8289. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  8290. };
  8291. #endif
  8292. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  8293. [0] = &nfs_v4_0_minor_ops,
  8294. #if defined(CONFIG_NFS_V4_1)
  8295. [1] = &nfs_v4_1_minor_ops,
  8296. #endif
  8297. #if defined(CONFIG_NFS_V4_2)
  8298. [2] = &nfs_v4_2_minor_ops,
  8299. #endif
  8300. };
  8301. static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
  8302. {
  8303. ssize_t error, error2;
  8304. error = generic_listxattr(dentry, list, size);
  8305. if (error < 0)
  8306. return error;
  8307. if (list) {
  8308. list += error;
  8309. size -= error;
  8310. }
  8311. error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
  8312. if (error2 < 0)
  8313. return error2;
  8314. return error + error2;
  8315. }
  8316. static const struct inode_operations nfs4_dir_inode_operations = {
  8317. .create = nfs_create,
  8318. .lookup = nfs_lookup,
  8319. .atomic_open = nfs_atomic_open,
  8320. .link = nfs_link,
  8321. .unlink = nfs_unlink,
  8322. .symlink = nfs_symlink,
  8323. .mkdir = nfs_mkdir,
  8324. .rmdir = nfs_rmdir,
  8325. .mknod = nfs_mknod,
  8326. .rename = nfs_rename,
  8327. .permission = nfs_permission,
  8328. .getattr = nfs_getattr,
  8329. .setattr = nfs_setattr,
  8330. .listxattr = nfs4_listxattr,
  8331. };
  8332. static const struct inode_operations nfs4_file_inode_operations = {
  8333. .permission = nfs_permission,
  8334. .getattr = nfs_getattr,
  8335. .setattr = nfs_setattr,
  8336. .listxattr = nfs4_listxattr,
  8337. };
  8338. const struct nfs_rpc_ops nfs_v4_clientops = {
  8339. .version = 4, /* protocol version */
  8340. .dentry_ops = &nfs4_dentry_operations,
  8341. .dir_inode_ops = &nfs4_dir_inode_operations,
  8342. .file_inode_ops = &nfs4_file_inode_operations,
  8343. .file_ops = &nfs4_file_operations,
  8344. .getroot = nfs4_proc_get_root,
  8345. .submount = nfs4_submount,
  8346. .try_mount = nfs4_try_mount,
  8347. .getattr = nfs4_proc_getattr,
  8348. .setattr = nfs4_proc_setattr,
  8349. .lookup = nfs4_proc_lookup,
  8350. .lookupp = nfs4_proc_lookupp,
  8351. .access = nfs4_proc_access,
  8352. .readlink = nfs4_proc_readlink,
  8353. .create = nfs4_proc_create,
  8354. .remove = nfs4_proc_remove,
  8355. .unlink_setup = nfs4_proc_unlink_setup,
  8356. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  8357. .unlink_done = nfs4_proc_unlink_done,
  8358. .rename_setup = nfs4_proc_rename_setup,
  8359. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  8360. .rename_done = nfs4_proc_rename_done,
  8361. .link = nfs4_proc_link,
  8362. .symlink = nfs4_proc_symlink,
  8363. .mkdir = nfs4_proc_mkdir,
  8364. .rmdir = nfs4_proc_remove,
  8365. .readdir = nfs4_proc_readdir,
  8366. .mknod = nfs4_proc_mknod,
  8367. .statfs = nfs4_proc_statfs,
  8368. .fsinfo = nfs4_proc_fsinfo,
  8369. .pathconf = nfs4_proc_pathconf,
  8370. .set_capabilities = nfs4_server_capabilities,
  8371. .decode_dirent = nfs4_decode_dirent,
  8372. .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
  8373. .read_setup = nfs4_proc_read_setup,
  8374. .read_done = nfs4_read_done,
  8375. .write_setup = nfs4_proc_write_setup,
  8376. .write_done = nfs4_write_done,
  8377. .commit_setup = nfs4_proc_commit_setup,
  8378. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  8379. .commit_done = nfs4_commit_done,
  8380. .lock = nfs4_proc_lock,
  8381. .clear_acl_cache = nfs4_zap_acl_attr,
  8382. .close_context = nfs4_close_context,
  8383. .open_context = nfs4_atomic_open,
  8384. .have_delegation = nfs4_have_delegation,
  8385. .return_delegation = nfs4_inode_return_delegation,
  8386. .alloc_client = nfs4_alloc_client,
  8387. .init_client = nfs4_init_client,
  8388. .free_client = nfs4_free_client,
  8389. .create_server = nfs4_create_server,
  8390. .clone_server = nfs_clone_server,
  8391. };
  8392. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  8393. .name = XATTR_NAME_NFSV4_ACL,
  8394. .list = nfs4_xattr_list_nfs4_acl,
  8395. .get = nfs4_xattr_get_nfs4_acl,
  8396. .set = nfs4_xattr_set_nfs4_acl,
  8397. };
  8398. const struct xattr_handler *nfs4_xattr_handlers[] = {
  8399. &nfs4_xattr_nfs4_acl_handler,
  8400. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  8401. &nfs4_xattr_nfs4_label_handler,
  8402. #endif
  8403. NULL
  8404. };
  8405. /*
  8406. * Local variables:
  8407. * c-basic-offset: 8
  8408. * End:
  8409. */