nfs4proc.c 240 KB

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