nfs4proc.c 240 KB

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