nfs4proc.c 229 KB

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