nfs4proc.c 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776
  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. #define NFSDBG_FACILITY NFSDBG_PROC
  66. #define NFS4_POLL_RETRY_MIN (HZ/10)
  67. #define NFS4_POLL_RETRY_MAX (15*HZ)
  68. struct nfs4_opendata;
  69. static int _nfs4_proc_open(struct nfs4_opendata *data);
  70. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  71. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  72. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  73. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  74. static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
  75. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  76. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  77. struct nfs_fattr *fattr, struct iattr *sattr,
  78. struct nfs4_state *state);
  79. #ifdef CONFIG_NFS_V4_1
  80. static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
  81. static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
  82. #endif
  83. /* Prevent leaks of NFSv4 errors into userland */
  84. static int nfs4_map_errors(int err)
  85. {
  86. if (err >= -1000)
  87. return err;
  88. switch (err) {
  89. case -NFS4ERR_RESOURCE:
  90. return -EREMOTEIO;
  91. case -NFS4ERR_WRONGSEC:
  92. return -EPERM;
  93. case -NFS4ERR_BADOWNER:
  94. case -NFS4ERR_BADNAME:
  95. return -EINVAL;
  96. case -NFS4ERR_SHARE_DENIED:
  97. return -EACCES;
  98. case -NFS4ERR_MINOR_VERS_MISMATCH:
  99. return -EPROTONOSUPPORT;
  100. case -NFS4ERR_ACCESS:
  101. return -EACCES;
  102. default:
  103. dprintk("%s could not handle NFSv4 error %d\n",
  104. __func__, -err);
  105. break;
  106. }
  107. return -EIO;
  108. }
  109. /*
  110. * This is our standard bitmap for GETATTR requests.
  111. */
  112. const u32 nfs4_fattr_bitmap[3] = {
  113. FATTR4_WORD0_TYPE
  114. | FATTR4_WORD0_CHANGE
  115. | FATTR4_WORD0_SIZE
  116. | FATTR4_WORD0_FSID
  117. | FATTR4_WORD0_FILEID,
  118. FATTR4_WORD1_MODE
  119. | FATTR4_WORD1_NUMLINKS
  120. | FATTR4_WORD1_OWNER
  121. | FATTR4_WORD1_OWNER_GROUP
  122. | FATTR4_WORD1_RAWDEV
  123. | FATTR4_WORD1_SPACE_USED
  124. | FATTR4_WORD1_TIME_ACCESS
  125. | FATTR4_WORD1_TIME_METADATA
  126. | FATTR4_WORD1_TIME_MODIFY
  127. };
  128. static const u32 nfs4_pnfs_open_bitmap[3] = {
  129. FATTR4_WORD0_TYPE
  130. | FATTR4_WORD0_CHANGE
  131. | FATTR4_WORD0_SIZE
  132. | FATTR4_WORD0_FSID
  133. | FATTR4_WORD0_FILEID,
  134. FATTR4_WORD1_MODE
  135. | FATTR4_WORD1_NUMLINKS
  136. | FATTR4_WORD1_OWNER
  137. | FATTR4_WORD1_OWNER_GROUP
  138. | FATTR4_WORD1_RAWDEV
  139. | FATTR4_WORD1_SPACE_USED
  140. | FATTR4_WORD1_TIME_ACCESS
  141. | FATTR4_WORD1_TIME_METADATA
  142. | FATTR4_WORD1_TIME_MODIFY,
  143. FATTR4_WORD2_MDSTHRESHOLD
  144. };
  145. static const u32 nfs4_open_noattr_bitmap[3] = {
  146. FATTR4_WORD0_TYPE
  147. | FATTR4_WORD0_CHANGE
  148. | FATTR4_WORD0_FILEID,
  149. };
  150. const u32 nfs4_statfs_bitmap[2] = {
  151. FATTR4_WORD0_FILES_AVAIL
  152. | FATTR4_WORD0_FILES_FREE
  153. | FATTR4_WORD0_FILES_TOTAL,
  154. FATTR4_WORD1_SPACE_AVAIL
  155. | FATTR4_WORD1_SPACE_FREE
  156. | FATTR4_WORD1_SPACE_TOTAL
  157. };
  158. const u32 nfs4_pathconf_bitmap[2] = {
  159. FATTR4_WORD0_MAXLINK
  160. | FATTR4_WORD0_MAXNAME,
  161. 0
  162. };
  163. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  164. | FATTR4_WORD0_MAXREAD
  165. | FATTR4_WORD0_MAXWRITE
  166. | FATTR4_WORD0_LEASE_TIME,
  167. FATTR4_WORD1_TIME_DELTA
  168. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  169. FATTR4_WORD2_LAYOUT_BLKSIZE
  170. };
  171. const u32 nfs4_fs_locations_bitmap[2] = {
  172. FATTR4_WORD0_TYPE
  173. | FATTR4_WORD0_CHANGE
  174. | FATTR4_WORD0_SIZE
  175. | FATTR4_WORD0_FSID
  176. | FATTR4_WORD0_FILEID
  177. | FATTR4_WORD0_FS_LOCATIONS,
  178. FATTR4_WORD1_MODE
  179. | FATTR4_WORD1_NUMLINKS
  180. | FATTR4_WORD1_OWNER
  181. | FATTR4_WORD1_OWNER_GROUP
  182. | FATTR4_WORD1_RAWDEV
  183. | FATTR4_WORD1_SPACE_USED
  184. | FATTR4_WORD1_TIME_ACCESS
  185. | FATTR4_WORD1_TIME_METADATA
  186. | FATTR4_WORD1_TIME_MODIFY
  187. | FATTR4_WORD1_MOUNTED_ON_FILEID
  188. };
  189. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  190. struct nfs4_readdir_arg *readdir)
  191. {
  192. __be32 *start, *p;
  193. if (cookie > 2) {
  194. readdir->cookie = cookie;
  195. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  196. return;
  197. }
  198. readdir->cookie = 0;
  199. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  200. if (cookie == 2)
  201. return;
  202. /*
  203. * NFSv4 servers do not return entries for '.' and '..'
  204. * Therefore, we fake these entries here. We let '.'
  205. * have cookie 0 and '..' have cookie 1. Note that
  206. * when talking to the server, we always send cookie 0
  207. * instead of 1 or 2.
  208. */
  209. start = p = kmap_atomic(*readdir->pages);
  210. if (cookie == 0) {
  211. *p++ = xdr_one; /* next */
  212. *p++ = xdr_zero; /* cookie, first word */
  213. *p++ = xdr_one; /* cookie, second word */
  214. *p++ = xdr_one; /* entry len */
  215. memcpy(p, ".\0\0\0", 4); /* entry */
  216. p++;
  217. *p++ = xdr_one; /* bitmap length */
  218. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  219. *p++ = htonl(8); /* attribute buffer length */
  220. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  221. }
  222. *p++ = xdr_one; /* next */
  223. *p++ = xdr_zero; /* cookie, first word */
  224. *p++ = xdr_two; /* cookie, second word */
  225. *p++ = xdr_two; /* entry len */
  226. memcpy(p, "..\0\0", 4); /* entry */
  227. p++;
  228. *p++ = xdr_one; /* bitmap length */
  229. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  230. *p++ = htonl(8); /* attribute buffer length */
  231. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  232. readdir->pgbase = (char *)p - (char *)start;
  233. readdir->count -= readdir->pgbase;
  234. kunmap_atomic(start);
  235. }
  236. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  237. {
  238. int res = 0;
  239. might_sleep();
  240. if (*timeout <= 0)
  241. *timeout = NFS4_POLL_RETRY_MIN;
  242. if (*timeout > NFS4_POLL_RETRY_MAX)
  243. *timeout = NFS4_POLL_RETRY_MAX;
  244. freezable_schedule_timeout_killable(*timeout);
  245. if (fatal_signal_pending(current))
  246. res = -ERESTARTSYS;
  247. *timeout <<= 1;
  248. return res;
  249. }
  250. /* This is the error handling routine for processes that are allowed
  251. * to sleep.
  252. */
  253. static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  254. {
  255. struct nfs_client *clp = server->nfs_client;
  256. struct nfs4_state *state = exception->state;
  257. struct inode *inode = exception->inode;
  258. int ret = errorcode;
  259. exception->retry = 0;
  260. switch(errorcode) {
  261. case 0:
  262. return 0;
  263. case -NFS4ERR_OPENMODE:
  264. if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
  265. nfs4_inode_return_delegation(inode);
  266. exception->retry = 1;
  267. return 0;
  268. }
  269. if (state == NULL)
  270. break;
  271. nfs4_schedule_stateid_recovery(server, state);
  272. goto wait_on_recovery;
  273. case -NFS4ERR_DELEG_REVOKED:
  274. case -NFS4ERR_ADMIN_REVOKED:
  275. case -NFS4ERR_BAD_STATEID:
  276. if (state == NULL)
  277. break;
  278. nfs_remove_bad_delegation(state->inode);
  279. nfs4_schedule_stateid_recovery(server, state);
  280. goto wait_on_recovery;
  281. case -NFS4ERR_EXPIRED:
  282. if (state != NULL)
  283. nfs4_schedule_stateid_recovery(server, state);
  284. case -NFS4ERR_STALE_STATEID:
  285. case -NFS4ERR_STALE_CLIENTID:
  286. nfs4_schedule_lease_recovery(clp);
  287. goto wait_on_recovery;
  288. #if defined(CONFIG_NFS_V4_1)
  289. case -NFS4ERR_BADSESSION:
  290. case -NFS4ERR_BADSLOT:
  291. case -NFS4ERR_BAD_HIGH_SLOT:
  292. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  293. case -NFS4ERR_DEADSESSION:
  294. case -NFS4ERR_SEQ_FALSE_RETRY:
  295. case -NFS4ERR_SEQ_MISORDERED:
  296. dprintk("%s ERROR: %d Reset session\n", __func__,
  297. errorcode);
  298. nfs4_schedule_session_recovery(clp->cl_session, errorcode);
  299. goto wait_on_recovery;
  300. #endif /* defined(CONFIG_NFS_V4_1) */
  301. case -NFS4ERR_FILE_OPEN:
  302. if (exception->timeout > HZ) {
  303. /* We have retried a decent amount, time to
  304. * fail
  305. */
  306. ret = -EBUSY;
  307. break;
  308. }
  309. case -NFS4ERR_GRACE:
  310. case -NFS4ERR_DELAY:
  311. case -EKEYEXPIRED:
  312. ret = nfs4_delay(server->client, &exception->timeout);
  313. if (ret != 0)
  314. break;
  315. case -NFS4ERR_RETRY_UNCACHED_REP:
  316. case -NFS4ERR_OLD_STATEID:
  317. exception->retry = 1;
  318. break;
  319. case -NFS4ERR_BADOWNER:
  320. /* The following works around a Linux server bug! */
  321. case -NFS4ERR_BADNAME:
  322. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  323. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  324. exception->retry = 1;
  325. printk(KERN_WARNING "NFS: v4 server %s "
  326. "does not accept raw "
  327. "uid/gids. "
  328. "Reenabling the idmapper.\n",
  329. server->nfs_client->cl_hostname);
  330. }
  331. }
  332. /* We failed to handle the error */
  333. return nfs4_map_errors(ret);
  334. wait_on_recovery:
  335. ret = nfs4_wait_clnt_recover(clp);
  336. if (ret == 0)
  337. exception->retry = 1;
  338. return ret;
  339. }
  340. static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  341. {
  342. spin_lock(&clp->cl_lock);
  343. if (time_before(clp->cl_last_renewal,timestamp))
  344. clp->cl_last_renewal = timestamp;
  345. spin_unlock(&clp->cl_lock);
  346. }
  347. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  348. {
  349. do_renew_lease(server->nfs_client, timestamp);
  350. }
  351. #if defined(CONFIG_NFS_V4_1)
  352. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  353. {
  354. struct nfs4_session *session;
  355. struct nfs4_slot_table *tbl;
  356. bool send_new_highest_used_slotid = false;
  357. if (!res->sr_slot) {
  358. /* just wake up the next guy waiting since
  359. * we may have not consumed a slot after all */
  360. dprintk("%s: No slot\n", __func__);
  361. return;
  362. }
  363. tbl = res->sr_slot->table;
  364. session = tbl->session;
  365. spin_lock(&tbl->slot_tbl_lock);
  366. /* Be nice to the server: try to ensure that the last transmitted
  367. * value for highest_user_slotid <= target_highest_slotid
  368. */
  369. if (tbl->highest_used_slotid > tbl->target_highest_slotid)
  370. send_new_highest_used_slotid = true;
  371. if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
  372. send_new_highest_used_slotid = false;
  373. goto out_unlock;
  374. }
  375. nfs4_free_slot(tbl, res->sr_slot);
  376. if (tbl->highest_used_slotid != NFS4_NO_SLOT)
  377. send_new_highest_used_slotid = false;
  378. out_unlock:
  379. spin_unlock(&tbl->slot_tbl_lock);
  380. res->sr_slot = NULL;
  381. if (send_new_highest_used_slotid)
  382. nfs41_server_notify_highest_slotid_update(session->clp);
  383. }
  384. static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  385. {
  386. struct nfs4_session *session;
  387. struct nfs4_slot *slot;
  388. unsigned long timestamp;
  389. struct nfs_client *clp;
  390. /*
  391. * sr_status remains 1 if an RPC level error occurred. The server
  392. * may or may not have processed the sequence operation..
  393. * Proceed as if the server received and processed the sequence
  394. * operation.
  395. */
  396. if (res->sr_status == 1)
  397. res->sr_status = NFS_OK;
  398. /* don't increment the sequence number if the task wasn't sent */
  399. if (!RPC_WAS_SENT(task))
  400. goto out;
  401. slot = res->sr_slot;
  402. session = slot->table->session;
  403. /* Check the SEQUENCE operation status */
  404. switch (res->sr_status) {
  405. case 0:
  406. /* Update the slot's sequence and clientid lease timer */
  407. ++slot->seq_nr;
  408. timestamp = slot->renewal_time;
  409. clp = session->clp;
  410. do_renew_lease(clp, timestamp);
  411. /* Check sequence flags */
  412. if (res->sr_status_flags != 0)
  413. nfs4_schedule_lease_recovery(clp);
  414. nfs41_update_target_slotid(slot->table, slot, res);
  415. break;
  416. case -NFS4ERR_DELAY:
  417. /* The server detected a resend of the RPC call and
  418. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  419. * of RFC5661.
  420. */
  421. dprintk("%s: slot=%u seq=%u: Operation in progress\n",
  422. __func__,
  423. slot->slot_nr,
  424. slot->seq_nr);
  425. goto out_retry;
  426. default:
  427. /* Just update the slot sequence no. */
  428. ++slot->seq_nr;
  429. }
  430. out:
  431. /* The session may be reset by one of the error handlers. */
  432. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  433. nfs41_sequence_free_slot(res);
  434. return 1;
  435. out_retry:
  436. if (!rpc_restart_call(task))
  437. goto out;
  438. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  439. return 0;
  440. }
  441. static int nfs4_sequence_done(struct rpc_task *task,
  442. struct nfs4_sequence_res *res)
  443. {
  444. if (res->sr_slot == NULL)
  445. return 1;
  446. return nfs41_sequence_done(task, res);
  447. }
  448. static void nfs41_init_sequence(struct nfs4_sequence_args *args,
  449. struct nfs4_sequence_res *res, int cache_reply)
  450. {
  451. args->sa_slot = NULL;
  452. args->sa_cache_this = 0;
  453. args->sa_privileged = 0;
  454. if (cache_reply)
  455. args->sa_cache_this = 1;
  456. res->sr_slot = NULL;
  457. }
  458. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  459. {
  460. args->sa_privileged = 1;
  461. }
  462. int nfs41_setup_sequence(struct nfs4_session *session,
  463. struct nfs4_sequence_args *args,
  464. struct nfs4_sequence_res *res,
  465. struct rpc_task *task)
  466. {
  467. struct nfs4_slot *slot;
  468. struct nfs4_slot_table *tbl;
  469. dprintk("--> %s\n", __func__);
  470. /* slot already allocated? */
  471. if (res->sr_slot != NULL)
  472. goto out_success;
  473. tbl = &session->fc_slot_table;
  474. task->tk_timeout = 0;
  475. spin_lock(&tbl->slot_tbl_lock);
  476. if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
  477. !args->sa_privileged) {
  478. /* The state manager will wait until the slot table is empty */
  479. dprintk("%s session is draining\n", __func__);
  480. goto out_sleep;
  481. }
  482. slot = nfs4_alloc_slot(tbl);
  483. if (IS_ERR(slot)) {
  484. /* If out of memory, try again in 1/4 second */
  485. if (slot == ERR_PTR(-ENOMEM))
  486. task->tk_timeout = HZ >> 2;
  487. dprintk("<-- %s: no free slots\n", __func__);
  488. goto out_sleep;
  489. }
  490. spin_unlock(&tbl->slot_tbl_lock);
  491. args->sa_slot = slot;
  492. dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
  493. slot->slot_nr, slot->seq_nr);
  494. res->sr_slot = slot;
  495. res->sr_status_flags = 0;
  496. /*
  497. * sr_status is only set in decode_sequence, and so will remain
  498. * set to 1 if an rpc level failure occurs.
  499. */
  500. res->sr_status = 1;
  501. out_success:
  502. rpc_call_start(task);
  503. return 0;
  504. out_sleep:
  505. /* Privileged tasks are queued with top priority */
  506. if (args->sa_privileged)
  507. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  508. NULL, RPC_PRIORITY_PRIVILEGED);
  509. else
  510. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  511. spin_unlock(&tbl->slot_tbl_lock);
  512. return -EAGAIN;
  513. }
  514. EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
  515. int nfs4_setup_sequence(const struct nfs_server *server,
  516. struct nfs4_sequence_args *args,
  517. struct nfs4_sequence_res *res,
  518. struct rpc_task *task)
  519. {
  520. struct nfs4_session *session = nfs4_get_session(server);
  521. int ret = 0;
  522. if (session == NULL) {
  523. rpc_call_start(task);
  524. goto out;
  525. }
  526. dprintk("--> %s clp %p session %p sr_slot %d\n",
  527. __func__, session->clp, session, res->sr_slot ?
  528. res->sr_slot->slot_nr : -1);
  529. ret = nfs41_setup_sequence(session, args, res, task);
  530. out:
  531. dprintk("<-- %s status=%d\n", __func__, ret);
  532. return ret;
  533. }
  534. struct nfs41_call_sync_data {
  535. const struct nfs_server *seq_server;
  536. struct nfs4_sequence_args *seq_args;
  537. struct nfs4_sequence_res *seq_res;
  538. };
  539. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  540. {
  541. struct nfs41_call_sync_data *data = calldata;
  542. struct nfs4_session *session = nfs4_get_session(data->seq_server);
  543. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  544. nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
  545. }
  546. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  547. {
  548. struct nfs41_call_sync_data *data = calldata;
  549. nfs41_sequence_done(task, data->seq_res);
  550. }
  551. static const struct rpc_call_ops nfs41_call_sync_ops = {
  552. .rpc_call_prepare = nfs41_call_sync_prepare,
  553. .rpc_call_done = nfs41_call_sync_done,
  554. };
  555. static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  556. struct nfs_server *server,
  557. struct rpc_message *msg,
  558. struct nfs4_sequence_args *args,
  559. struct nfs4_sequence_res *res)
  560. {
  561. int ret;
  562. struct rpc_task *task;
  563. struct nfs41_call_sync_data data = {
  564. .seq_server = server,
  565. .seq_args = args,
  566. .seq_res = res,
  567. };
  568. struct rpc_task_setup task_setup = {
  569. .rpc_client = clnt,
  570. .rpc_message = msg,
  571. .callback_ops = &nfs41_call_sync_ops,
  572. .callback_data = &data
  573. };
  574. task = rpc_run_task(&task_setup);
  575. if (IS_ERR(task))
  576. ret = PTR_ERR(task);
  577. else {
  578. ret = task->tk_status;
  579. rpc_put_task(task);
  580. }
  581. return ret;
  582. }
  583. #else
  584. static
  585. void nfs41_init_sequence(struct nfs4_sequence_args *args,
  586. struct nfs4_sequence_res *res, int cache_reply)
  587. {
  588. }
  589. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  590. {
  591. }
  592. static int nfs4_sequence_done(struct rpc_task *task,
  593. struct nfs4_sequence_res *res)
  594. {
  595. return 1;
  596. }
  597. #endif /* CONFIG_NFS_V4_1 */
  598. static
  599. int _nfs4_call_sync(struct rpc_clnt *clnt,
  600. struct nfs_server *server,
  601. struct rpc_message *msg,
  602. struct nfs4_sequence_args *args,
  603. struct nfs4_sequence_res *res)
  604. {
  605. return rpc_call_sync(clnt, msg, 0);
  606. }
  607. static
  608. int nfs4_call_sync(struct rpc_clnt *clnt,
  609. struct nfs_server *server,
  610. struct rpc_message *msg,
  611. struct nfs4_sequence_args *args,
  612. struct nfs4_sequence_res *res,
  613. int cache_reply)
  614. {
  615. nfs41_init_sequence(args, res, cache_reply);
  616. return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
  617. args, res);
  618. }
  619. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  620. {
  621. struct nfs_inode *nfsi = NFS_I(dir);
  622. spin_lock(&dir->i_lock);
  623. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  624. if (!cinfo->atomic || cinfo->before != dir->i_version)
  625. nfs_force_lookup_revalidate(dir);
  626. dir->i_version = cinfo->after;
  627. spin_unlock(&dir->i_lock);
  628. }
  629. struct nfs4_opendata {
  630. struct kref kref;
  631. struct nfs_openargs o_arg;
  632. struct nfs_openres o_res;
  633. struct nfs_open_confirmargs c_arg;
  634. struct nfs_open_confirmres c_res;
  635. struct nfs4_string owner_name;
  636. struct nfs4_string group_name;
  637. struct nfs_fattr f_attr;
  638. struct dentry *dir;
  639. struct dentry *dentry;
  640. struct nfs4_state_owner *owner;
  641. struct nfs4_state *state;
  642. struct iattr attrs;
  643. unsigned long timestamp;
  644. unsigned int rpc_done : 1;
  645. int rpc_status;
  646. int cancelled;
  647. };
  648. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  649. {
  650. p->o_res.f_attr = &p->f_attr;
  651. p->o_res.seqid = p->o_arg.seqid;
  652. p->c_res.seqid = p->c_arg.seqid;
  653. p->o_res.server = p->o_arg.server;
  654. p->o_res.access_request = p->o_arg.access;
  655. nfs_fattr_init(&p->f_attr);
  656. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  657. }
  658. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  659. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  660. const struct iattr *attrs,
  661. gfp_t gfp_mask)
  662. {
  663. struct dentry *parent = dget_parent(dentry);
  664. struct inode *dir = parent->d_inode;
  665. struct nfs_server *server = NFS_SERVER(dir);
  666. struct nfs4_opendata *p;
  667. p = kzalloc(sizeof(*p), gfp_mask);
  668. if (p == NULL)
  669. goto err;
  670. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
  671. if (p->o_arg.seqid == NULL)
  672. goto err_free;
  673. nfs_sb_active(dentry->d_sb);
  674. p->dentry = dget(dentry);
  675. p->dir = parent;
  676. p->owner = sp;
  677. atomic_inc(&sp->so_count);
  678. p->o_arg.fh = NFS_FH(dir);
  679. p->o_arg.open_flags = flags;
  680. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  681. /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
  682. * will return permission denied for all bits until close */
  683. if (!(flags & O_EXCL)) {
  684. /* ask server to check for all possible rights as results
  685. * are cached */
  686. p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
  687. NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
  688. }
  689. p->o_arg.clientid = server->nfs_client->cl_clientid;
  690. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  691. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  692. p->o_arg.name = &dentry->d_name;
  693. p->o_arg.server = server;
  694. p->o_arg.bitmask = server->attr_bitmask;
  695. p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
  696. p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
  697. if (attrs != NULL && attrs->ia_valid != 0) {
  698. __be32 verf[2];
  699. p->o_arg.u.attrs = &p->attrs;
  700. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  701. verf[0] = jiffies;
  702. verf[1] = current->pid;
  703. memcpy(p->o_arg.u.verifier.data, verf,
  704. sizeof(p->o_arg.u.verifier.data));
  705. }
  706. p->c_arg.fh = &p->o_res.fh;
  707. p->c_arg.stateid = &p->o_res.stateid;
  708. p->c_arg.seqid = p->o_arg.seqid;
  709. nfs4_init_opendata_res(p);
  710. kref_init(&p->kref);
  711. return p;
  712. err_free:
  713. kfree(p);
  714. err:
  715. dput(parent);
  716. return NULL;
  717. }
  718. static void nfs4_opendata_free(struct kref *kref)
  719. {
  720. struct nfs4_opendata *p = container_of(kref,
  721. struct nfs4_opendata, kref);
  722. struct super_block *sb = p->dentry->d_sb;
  723. nfs_free_seqid(p->o_arg.seqid);
  724. if (p->state != NULL)
  725. nfs4_put_open_state(p->state);
  726. nfs4_put_state_owner(p->owner);
  727. dput(p->dir);
  728. dput(p->dentry);
  729. nfs_sb_deactive(sb);
  730. nfs_fattr_free_names(&p->f_attr);
  731. kfree(p);
  732. }
  733. static void nfs4_opendata_put(struct nfs4_opendata *p)
  734. {
  735. if (p != NULL)
  736. kref_put(&p->kref, nfs4_opendata_free);
  737. }
  738. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  739. {
  740. int ret;
  741. ret = rpc_wait_for_completion_task(task);
  742. return ret;
  743. }
  744. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  745. {
  746. int ret = 0;
  747. if (open_mode & (O_EXCL|O_TRUNC))
  748. goto out;
  749. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  750. case FMODE_READ:
  751. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  752. && state->n_rdonly != 0;
  753. break;
  754. case FMODE_WRITE:
  755. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  756. && state->n_wronly != 0;
  757. break;
  758. case FMODE_READ|FMODE_WRITE:
  759. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  760. && state->n_rdwr != 0;
  761. }
  762. out:
  763. return ret;
  764. }
  765. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  766. {
  767. if (delegation == NULL)
  768. return 0;
  769. if ((delegation->type & fmode) != fmode)
  770. return 0;
  771. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  772. return 0;
  773. nfs_mark_delegation_referenced(delegation);
  774. return 1;
  775. }
  776. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  777. {
  778. switch (fmode) {
  779. case FMODE_WRITE:
  780. state->n_wronly++;
  781. break;
  782. case FMODE_READ:
  783. state->n_rdonly++;
  784. break;
  785. case FMODE_READ|FMODE_WRITE:
  786. state->n_rdwr++;
  787. }
  788. nfs4_state_set_mode_locked(state, state->state | fmode);
  789. }
  790. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  791. {
  792. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  793. nfs4_stateid_copy(&state->stateid, stateid);
  794. nfs4_stateid_copy(&state->open_stateid, stateid);
  795. switch (fmode) {
  796. case FMODE_READ:
  797. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  798. break;
  799. case FMODE_WRITE:
  800. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  801. break;
  802. case FMODE_READ|FMODE_WRITE:
  803. set_bit(NFS_O_RDWR_STATE, &state->flags);
  804. }
  805. }
  806. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  807. {
  808. write_seqlock(&state->seqlock);
  809. nfs_set_open_stateid_locked(state, stateid, fmode);
  810. write_sequnlock(&state->seqlock);
  811. }
  812. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  813. {
  814. /*
  815. * Protect the call to nfs4_state_set_mode_locked and
  816. * serialise the stateid update
  817. */
  818. write_seqlock(&state->seqlock);
  819. if (deleg_stateid != NULL) {
  820. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  821. set_bit(NFS_DELEGATED_STATE, &state->flags);
  822. }
  823. if (open_stateid != NULL)
  824. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  825. write_sequnlock(&state->seqlock);
  826. spin_lock(&state->owner->so_lock);
  827. update_open_stateflags(state, fmode);
  828. spin_unlock(&state->owner->so_lock);
  829. }
  830. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  831. {
  832. struct nfs_inode *nfsi = NFS_I(state->inode);
  833. struct nfs_delegation *deleg_cur;
  834. int ret = 0;
  835. fmode &= (FMODE_READ|FMODE_WRITE);
  836. rcu_read_lock();
  837. deleg_cur = rcu_dereference(nfsi->delegation);
  838. if (deleg_cur == NULL)
  839. goto no_delegation;
  840. spin_lock(&deleg_cur->lock);
  841. if (nfsi->delegation != deleg_cur ||
  842. (deleg_cur->type & fmode) != fmode)
  843. goto no_delegation_unlock;
  844. if (delegation == NULL)
  845. delegation = &deleg_cur->stateid;
  846. else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
  847. goto no_delegation_unlock;
  848. nfs_mark_delegation_referenced(deleg_cur);
  849. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  850. ret = 1;
  851. no_delegation_unlock:
  852. spin_unlock(&deleg_cur->lock);
  853. no_delegation:
  854. rcu_read_unlock();
  855. if (!ret && open_stateid != NULL) {
  856. __update_open_stateid(state, open_stateid, NULL, fmode);
  857. ret = 1;
  858. }
  859. return ret;
  860. }
  861. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  862. {
  863. struct nfs_delegation *delegation;
  864. rcu_read_lock();
  865. delegation = rcu_dereference(NFS_I(inode)->delegation);
  866. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  867. rcu_read_unlock();
  868. return;
  869. }
  870. rcu_read_unlock();
  871. nfs4_inode_return_delegation(inode);
  872. }
  873. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  874. {
  875. struct nfs4_state *state = opendata->state;
  876. struct nfs_inode *nfsi = NFS_I(state->inode);
  877. struct nfs_delegation *delegation;
  878. int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
  879. fmode_t fmode = opendata->o_arg.fmode;
  880. nfs4_stateid stateid;
  881. int ret = -EAGAIN;
  882. for (;;) {
  883. if (can_open_cached(state, fmode, open_mode)) {
  884. spin_lock(&state->owner->so_lock);
  885. if (can_open_cached(state, fmode, open_mode)) {
  886. update_open_stateflags(state, fmode);
  887. spin_unlock(&state->owner->so_lock);
  888. goto out_return_state;
  889. }
  890. spin_unlock(&state->owner->so_lock);
  891. }
  892. rcu_read_lock();
  893. delegation = rcu_dereference(nfsi->delegation);
  894. if (!can_open_delegated(delegation, fmode)) {
  895. rcu_read_unlock();
  896. break;
  897. }
  898. /* Save the delegation */
  899. nfs4_stateid_copy(&stateid, &delegation->stateid);
  900. rcu_read_unlock();
  901. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  902. if (ret != 0)
  903. goto out;
  904. ret = -EAGAIN;
  905. /* Try to update the stateid using the delegation */
  906. if (update_open_stateid(state, NULL, &stateid, fmode))
  907. goto out_return_state;
  908. }
  909. out:
  910. return ERR_PTR(ret);
  911. out_return_state:
  912. atomic_inc(&state->count);
  913. return state;
  914. }
  915. static void
  916. nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
  917. {
  918. struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
  919. struct nfs_delegation *delegation;
  920. int delegation_flags = 0;
  921. rcu_read_lock();
  922. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  923. if (delegation)
  924. delegation_flags = delegation->flags;
  925. rcu_read_unlock();
  926. if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
  927. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  928. "returning a delegation for "
  929. "OPEN(CLAIM_DELEGATE_CUR)\n",
  930. clp->cl_hostname);
  931. } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  932. nfs_inode_set_delegation(state->inode,
  933. data->owner->so_cred,
  934. &data->o_res);
  935. else
  936. nfs_inode_reclaim_delegation(state->inode,
  937. data->owner->so_cred,
  938. &data->o_res);
  939. }
  940. /*
  941. * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
  942. * and update the nfs4_state.
  943. */
  944. static struct nfs4_state *
  945. _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
  946. {
  947. struct inode *inode = data->state->inode;
  948. struct nfs4_state *state = data->state;
  949. int ret;
  950. if (!data->rpc_done) {
  951. ret = data->rpc_status;
  952. goto err;
  953. }
  954. ret = -ESTALE;
  955. if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
  956. !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
  957. !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
  958. goto err;
  959. ret = -ENOMEM;
  960. state = nfs4_get_open_state(inode, data->owner);
  961. if (state == NULL)
  962. goto err;
  963. ret = nfs_refresh_inode(inode, &data->f_attr);
  964. if (ret)
  965. goto err;
  966. if (data->o_res.delegation_type != 0)
  967. nfs4_opendata_check_deleg(data, state);
  968. update_open_stateid(state, &data->o_res.stateid, NULL,
  969. data->o_arg.fmode);
  970. return state;
  971. err:
  972. return ERR_PTR(ret);
  973. }
  974. static struct nfs4_state *
  975. _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  976. {
  977. struct inode *inode;
  978. struct nfs4_state *state = NULL;
  979. int ret;
  980. if (!data->rpc_done) {
  981. state = nfs4_try_open_cached(data);
  982. goto out;
  983. }
  984. ret = -EAGAIN;
  985. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  986. goto err;
  987. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  988. ret = PTR_ERR(inode);
  989. if (IS_ERR(inode))
  990. goto err;
  991. ret = -ENOMEM;
  992. state = nfs4_get_open_state(inode, data->owner);
  993. if (state == NULL)
  994. goto err_put_inode;
  995. if (data->o_res.delegation_type != 0)
  996. nfs4_opendata_check_deleg(data, state);
  997. update_open_stateid(state, &data->o_res.stateid, NULL,
  998. data->o_arg.fmode);
  999. iput(inode);
  1000. out:
  1001. return state;
  1002. err_put_inode:
  1003. iput(inode);
  1004. err:
  1005. return ERR_PTR(ret);
  1006. }
  1007. static struct nfs4_state *
  1008. nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1009. {
  1010. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
  1011. return _nfs4_opendata_reclaim_to_nfs4_state(data);
  1012. return _nfs4_opendata_to_nfs4_state(data);
  1013. }
  1014. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  1015. {
  1016. struct nfs_inode *nfsi = NFS_I(state->inode);
  1017. struct nfs_open_context *ctx;
  1018. spin_lock(&state->inode->i_lock);
  1019. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1020. if (ctx->state != state)
  1021. continue;
  1022. get_nfs_open_context(ctx);
  1023. spin_unlock(&state->inode->i_lock);
  1024. return ctx;
  1025. }
  1026. spin_unlock(&state->inode->i_lock);
  1027. return ERR_PTR(-ENOENT);
  1028. }
  1029. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
  1030. {
  1031. struct nfs4_opendata *opendata;
  1032. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
  1033. if (opendata == NULL)
  1034. return ERR_PTR(-ENOMEM);
  1035. opendata->state = state;
  1036. atomic_inc(&state->count);
  1037. return opendata;
  1038. }
  1039. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  1040. {
  1041. struct nfs4_state *newstate;
  1042. int ret;
  1043. opendata->o_arg.open_flags = 0;
  1044. opendata->o_arg.fmode = fmode;
  1045. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1046. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1047. nfs4_init_opendata_res(opendata);
  1048. ret = _nfs4_recover_proc_open(opendata);
  1049. if (ret != 0)
  1050. return ret;
  1051. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1052. if (IS_ERR(newstate))
  1053. return PTR_ERR(newstate);
  1054. nfs4_close_state(newstate, fmode);
  1055. *res = newstate;
  1056. return 0;
  1057. }
  1058. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1059. {
  1060. struct nfs4_state *newstate;
  1061. int ret;
  1062. /* memory barrier prior to reading state->n_* */
  1063. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1064. smp_rmb();
  1065. if (state->n_rdwr != 0) {
  1066. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1067. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  1068. if (ret != 0)
  1069. return ret;
  1070. if (newstate != state)
  1071. return -ESTALE;
  1072. }
  1073. if (state->n_wronly != 0) {
  1074. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1075. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  1076. if (ret != 0)
  1077. return ret;
  1078. if (newstate != state)
  1079. return -ESTALE;
  1080. }
  1081. if (state->n_rdonly != 0) {
  1082. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1083. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  1084. if (ret != 0)
  1085. return ret;
  1086. if (newstate != state)
  1087. return -ESTALE;
  1088. }
  1089. /*
  1090. * We may have performed cached opens for all three recoveries.
  1091. * Check if we need to update the current stateid.
  1092. */
  1093. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1094. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1095. write_seqlock(&state->seqlock);
  1096. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1097. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1098. write_sequnlock(&state->seqlock);
  1099. }
  1100. return 0;
  1101. }
  1102. /*
  1103. * OPEN_RECLAIM:
  1104. * reclaim state on the server after a reboot.
  1105. */
  1106. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1107. {
  1108. struct nfs_delegation *delegation;
  1109. struct nfs4_opendata *opendata;
  1110. fmode_t delegation_type = 0;
  1111. int status;
  1112. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1113. if (IS_ERR(opendata))
  1114. return PTR_ERR(opendata);
  1115. opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
  1116. opendata->o_arg.fh = NFS_FH(state->inode);
  1117. rcu_read_lock();
  1118. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1119. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1120. delegation_type = delegation->type;
  1121. rcu_read_unlock();
  1122. opendata->o_arg.u.delegation_type = delegation_type;
  1123. status = nfs4_open_recover(opendata, state);
  1124. nfs4_opendata_put(opendata);
  1125. return status;
  1126. }
  1127. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1128. {
  1129. struct nfs_server *server = NFS_SERVER(state->inode);
  1130. struct nfs4_exception exception = { };
  1131. int err;
  1132. do {
  1133. err = _nfs4_do_open_reclaim(ctx, state);
  1134. if (err != -NFS4ERR_DELAY)
  1135. break;
  1136. nfs4_handle_exception(server, err, &exception);
  1137. } while (exception.retry);
  1138. return err;
  1139. }
  1140. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1141. {
  1142. struct nfs_open_context *ctx;
  1143. int ret;
  1144. ctx = nfs4_state_find_open_context(state);
  1145. if (IS_ERR(ctx))
  1146. return PTR_ERR(ctx);
  1147. ret = nfs4_do_open_reclaim(ctx, state);
  1148. put_nfs_open_context(ctx);
  1149. return ret;
  1150. }
  1151. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1152. {
  1153. struct nfs4_opendata *opendata;
  1154. int ret;
  1155. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1156. if (IS_ERR(opendata))
  1157. return PTR_ERR(opendata);
  1158. opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1159. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  1160. ret = nfs4_open_recover(opendata, state);
  1161. nfs4_opendata_put(opendata);
  1162. return ret;
  1163. }
  1164. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1165. {
  1166. struct nfs4_exception exception = { };
  1167. struct nfs_server *server = NFS_SERVER(state->inode);
  1168. int err;
  1169. do {
  1170. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  1171. switch (err) {
  1172. case 0:
  1173. case -ENOENT:
  1174. case -ESTALE:
  1175. goto out;
  1176. case -NFS4ERR_BADSESSION:
  1177. case -NFS4ERR_BADSLOT:
  1178. case -NFS4ERR_BAD_HIGH_SLOT:
  1179. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1180. case -NFS4ERR_DEADSESSION:
  1181. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  1182. goto out;
  1183. case -NFS4ERR_STALE_CLIENTID:
  1184. case -NFS4ERR_STALE_STATEID:
  1185. case -NFS4ERR_EXPIRED:
  1186. /* Don't recall a delegation if it was lost */
  1187. nfs4_schedule_lease_recovery(server->nfs_client);
  1188. goto out;
  1189. case -ERESTARTSYS:
  1190. /*
  1191. * The show must go on: exit, but mark the
  1192. * stateid as needing recovery.
  1193. */
  1194. case -NFS4ERR_DELEG_REVOKED:
  1195. case -NFS4ERR_ADMIN_REVOKED:
  1196. case -NFS4ERR_BAD_STATEID:
  1197. nfs_inode_find_state_and_recover(state->inode,
  1198. stateid);
  1199. nfs4_schedule_stateid_recovery(server, state);
  1200. case -EKEYEXPIRED:
  1201. /*
  1202. * User RPCSEC_GSS context has expired.
  1203. * We cannot recover this stateid now, so
  1204. * skip it and allow recovery thread to
  1205. * proceed.
  1206. */
  1207. case -ENOMEM:
  1208. err = 0;
  1209. goto out;
  1210. }
  1211. err = nfs4_handle_exception(server, err, &exception);
  1212. } while (exception.retry);
  1213. out:
  1214. return err;
  1215. }
  1216. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1217. {
  1218. struct nfs4_opendata *data = calldata;
  1219. data->rpc_status = task->tk_status;
  1220. if (data->rpc_status == 0) {
  1221. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  1222. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1223. renew_lease(data->o_res.server, data->timestamp);
  1224. data->rpc_done = 1;
  1225. }
  1226. }
  1227. static void nfs4_open_confirm_release(void *calldata)
  1228. {
  1229. struct nfs4_opendata *data = calldata;
  1230. struct nfs4_state *state = NULL;
  1231. /* If this request hasn't been cancelled, do nothing */
  1232. if (data->cancelled == 0)
  1233. goto out_free;
  1234. /* In case of error, no cleanup! */
  1235. if (!data->rpc_done)
  1236. goto out_free;
  1237. state = nfs4_opendata_to_nfs4_state(data);
  1238. if (!IS_ERR(state))
  1239. nfs4_close_state(state, data->o_arg.fmode);
  1240. out_free:
  1241. nfs4_opendata_put(data);
  1242. }
  1243. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1244. .rpc_call_done = nfs4_open_confirm_done,
  1245. .rpc_release = nfs4_open_confirm_release,
  1246. };
  1247. /*
  1248. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1249. */
  1250. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1251. {
  1252. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1253. struct rpc_task *task;
  1254. struct rpc_message msg = {
  1255. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1256. .rpc_argp = &data->c_arg,
  1257. .rpc_resp = &data->c_res,
  1258. .rpc_cred = data->owner->so_cred,
  1259. };
  1260. struct rpc_task_setup task_setup_data = {
  1261. .rpc_client = server->client,
  1262. .rpc_message = &msg,
  1263. .callback_ops = &nfs4_open_confirm_ops,
  1264. .callback_data = data,
  1265. .workqueue = nfsiod_workqueue,
  1266. .flags = RPC_TASK_ASYNC,
  1267. };
  1268. int status;
  1269. kref_get(&data->kref);
  1270. data->rpc_done = 0;
  1271. data->rpc_status = 0;
  1272. data->timestamp = jiffies;
  1273. task = rpc_run_task(&task_setup_data);
  1274. if (IS_ERR(task))
  1275. return PTR_ERR(task);
  1276. status = nfs4_wait_for_completion_rpc_task(task);
  1277. if (status != 0) {
  1278. data->cancelled = 1;
  1279. smp_wmb();
  1280. } else
  1281. status = data->rpc_status;
  1282. rpc_put_task(task);
  1283. return status;
  1284. }
  1285. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1286. {
  1287. struct nfs4_opendata *data = calldata;
  1288. struct nfs4_state_owner *sp = data->owner;
  1289. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1290. return;
  1291. /*
  1292. * Check if we still need to send an OPEN call, or if we can use
  1293. * a delegation instead.
  1294. */
  1295. if (data->state != NULL) {
  1296. struct nfs_delegation *delegation;
  1297. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1298. goto out_no_action;
  1299. rcu_read_lock();
  1300. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1301. if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
  1302. can_open_delegated(delegation, data->o_arg.fmode))
  1303. goto unlock_no_action;
  1304. rcu_read_unlock();
  1305. }
  1306. /* Update client id. */
  1307. data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
  1308. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1309. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1310. data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
  1311. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1312. }
  1313. data->timestamp = jiffies;
  1314. if (nfs4_setup_sequence(data->o_arg.server,
  1315. &data->o_arg.seq_args,
  1316. &data->o_res.seq_res,
  1317. task) != 0)
  1318. nfs_release_seqid(data->o_arg.seqid);
  1319. return;
  1320. unlock_no_action:
  1321. rcu_read_unlock();
  1322. out_no_action:
  1323. task->tk_action = NULL;
  1324. nfs4_sequence_done(task, &data->o_res.seq_res);
  1325. }
  1326. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1327. {
  1328. struct nfs4_opendata *data = calldata;
  1329. data->rpc_status = task->tk_status;
  1330. if (!nfs4_sequence_done(task, &data->o_res.seq_res))
  1331. return;
  1332. if (task->tk_status == 0) {
  1333. if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
  1334. switch (data->o_res.f_attr->mode & S_IFMT) {
  1335. case S_IFREG:
  1336. break;
  1337. case S_IFLNK:
  1338. data->rpc_status = -ELOOP;
  1339. break;
  1340. case S_IFDIR:
  1341. data->rpc_status = -EISDIR;
  1342. break;
  1343. default:
  1344. data->rpc_status = -ENOTDIR;
  1345. }
  1346. }
  1347. renew_lease(data->o_res.server, data->timestamp);
  1348. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1349. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1350. }
  1351. data->rpc_done = 1;
  1352. }
  1353. static void nfs4_open_release(void *calldata)
  1354. {
  1355. struct nfs4_opendata *data = calldata;
  1356. struct nfs4_state *state = NULL;
  1357. /* If this request hasn't been cancelled, do nothing */
  1358. if (data->cancelled == 0)
  1359. goto out_free;
  1360. /* In case of error, no cleanup! */
  1361. if (data->rpc_status != 0 || !data->rpc_done)
  1362. goto out_free;
  1363. /* In case we need an open_confirm, no cleanup! */
  1364. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1365. goto out_free;
  1366. state = nfs4_opendata_to_nfs4_state(data);
  1367. if (!IS_ERR(state))
  1368. nfs4_close_state(state, data->o_arg.fmode);
  1369. out_free:
  1370. nfs4_opendata_put(data);
  1371. }
  1372. static const struct rpc_call_ops nfs4_open_ops = {
  1373. .rpc_call_prepare = nfs4_open_prepare,
  1374. .rpc_call_done = nfs4_open_done,
  1375. .rpc_release = nfs4_open_release,
  1376. };
  1377. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  1378. {
  1379. struct inode *dir = data->dir->d_inode;
  1380. struct nfs_server *server = NFS_SERVER(dir);
  1381. struct nfs_openargs *o_arg = &data->o_arg;
  1382. struct nfs_openres *o_res = &data->o_res;
  1383. struct rpc_task *task;
  1384. struct rpc_message msg = {
  1385. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1386. .rpc_argp = o_arg,
  1387. .rpc_resp = o_res,
  1388. .rpc_cred = data->owner->so_cred,
  1389. };
  1390. struct rpc_task_setup task_setup_data = {
  1391. .rpc_client = server->client,
  1392. .rpc_message = &msg,
  1393. .callback_ops = &nfs4_open_ops,
  1394. .callback_data = data,
  1395. .workqueue = nfsiod_workqueue,
  1396. .flags = RPC_TASK_ASYNC,
  1397. };
  1398. int status;
  1399. nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
  1400. kref_get(&data->kref);
  1401. data->rpc_done = 0;
  1402. data->rpc_status = 0;
  1403. data->cancelled = 0;
  1404. if (isrecover)
  1405. nfs4_set_sequence_privileged(&o_arg->seq_args);
  1406. task = rpc_run_task(&task_setup_data);
  1407. if (IS_ERR(task))
  1408. return PTR_ERR(task);
  1409. status = nfs4_wait_for_completion_rpc_task(task);
  1410. if (status != 0) {
  1411. data->cancelled = 1;
  1412. smp_wmb();
  1413. } else
  1414. status = data->rpc_status;
  1415. rpc_put_task(task);
  1416. return status;
  1417. }
  1418. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  1419. {
  1420. struct inode *dir = data->dir->d_inode;
  1421. struct nfs_openres *o_res = &data->o_res;
  1422. int status;
  1423. status = nfs4_run_open_task(data, 1);
  1424. if (status != 0 || !data->rpc_done)
  1425. return status;
  1426. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  1427. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1428. status = _nfs4_proc_open_confirm(data);
  1429. if (status != 0)
  1430. return status;
  1431. }
  1432. return status;
  1433. }
  1434. static int nfs4_opendata_access(struct rpc_cred *cred,
  1435. struct nfs4_opendata *opendata,
  1436. struct nfs4_state *state, fmode_t fmode)
  1437. {
  1438. struct nfs_access_entry cache;
  1439. u32 mask;
  1440. /* access call failed or for some reason the server doesn't
  1441. * support any access modes -- defer access call until later */
  1442. if (opendata->o_res.access_supported == 0)
  1443. return 0;
  1444. mask = 0;
  1445. /* don't check MAY_WRITE - a newly created file may not have
  1446. * write mode bits, but POSIX allows the creating process to write */
  1447. if (fmode & FMODE_READ)
  1448. mask |= MAY_READ;
  1449. if (fmode & FMODE_EXEC)
  1450. mask |= MAY_EXEC;
  1451. cache.cred = cred;
  1452. cache.jiffies = jiffies;
  1453. nfs_access_set_mask(&cache, opendata->o_res.access_result);
  1454. nfs_access_add_cache(state->inode, &cache);
  1455. if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
  1456. return 0;
  1457. /* even though OPEN succeeded, access is denied. Close the file */
  1458. nfs4_close_state(state, fmode);
  1459. return -EACCES;
  1460. }
  1461. /*
  1462. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1463. */
  1464. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1465. {
  1466. struct inode *dir = data->dir->d_inode;
  1467. struct nfs_server *server = NFS_SERVER(dir);
  1468. struct nfs_openargs *o_arg = &data->o_arg;
  1469. struct nfs_openres *o_res = &data->o_res;
  1470. int status;
  1471. status = nfs4_run_open_task(data, 0);
  1472. if (!data->rpc_done)
  1473. return status;
  1474. if (status != 0) {
  1475. if (status == -NFS4ERR_BADNAME &&
  1476. !(o_arg->open_flags & O_CREAT))
  1477. return -ENOENT;
  1478. return status;
  1479. }
  1480. nfs_fattr_map_and_free_names(server, &data->f_attr);
  1481. if (o_arg->open_flags & O_CREAT)
  1482. update_changeattr(dir, &o_res->cinfo);
  1483. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  1484. server->caps &= ~NFS_CAP_POSIX_LOCK;
  1485. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1486. status = _nfs4_proc_open_confirm(data);
  1487. if (status != 0)
  1488. return status;
  1489. }
  1490. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1491. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1492. return 0;
  1493. }
  1494. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1495. {
  1496. return nfs4_client_recover_expired_lease(server->nfs_client);
  1497. }
  1498. /*
  1499. * OPEN_EXPIRED:
  1500. * reclaim state on the server after a network partition.
  1501. * Assumes caller holds the appropriate lock
  1502. */
  1503. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1504. {
  1505. struct nfs4_opendata *opendata;
  1506. int ret;
  1507. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1508. if (IS_ERR(opendata))
  1509. return PTR_ERR(opendata);
  1510. ret = nfs4_open_recover(opendata, state);
  1511. if (ret == -ESTALE)
  1512. d_drop(ctx->dentry);
  1513. nfs4_opendata_put(opendata);
  1514. return ret;
  1515. }
  1516. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1517. {
  1518. struct nfs_server *server = NFS_SERVER(state->inode);
  1519. struct nfs4_exception exception = { };
  1520. int err;
  1521. do {
  1522. err = _nfs4_open_expired(ctx, state);
  1523. switch (err) {
  1524. default:
  1525. goto out;
  1526. case -NFS4ERR_GRACE:
  1527. case -NFS4ERR_DELAY:
  1528. nfs4_handle_exception(server, err, &exception);
  1529. err = 0;
  1530. }
  1531. } while (exception.retry);
  1532. out:
  1533. return err;
  1534. }
  1535. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1536. {
  1537. struct nfs_open_context *ctx;
  1538. int ret;
  1539. ctx = nfs4_state_find_open_context(state);
  1540. if (IS_ERR(ctx))
  1541. return PTR_ERR(ctx);
  1542. ret = nfs4_do_open_expired(ctx, state);
  1543. put_nfs_open_context(ctx);
  1544. return ret;
  1545. }
  1546. #if defined(CONFIG_NFS_V4_1)
  1547. static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
  1548. {
  1549. struct nfs_server *server = NFS_SERVER(state->inode);
  1550. nfs4_stateid *stateid = &state->stateid;
  1551. int status;
  1552. /* If a state reset has been done, test_stateid is unneeded */
  1553. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1554. return;
  1555. status = nfs41_test_stateid(server, stateid);
  1556. if (status != NFS_OK) {
  1557. /* Free the stateid unless the server explicitly
  1558. * informs us the stateid is unrecognized. */
  1559. if (status != -NFS4ERR_BAD_STATEID)
  1560. nfs41_free_stateid(server, stateid);
  1561. nfs_remove_bad_delegation(state->inode);
  1562. write_seqlock(&state->seqlock);
  1563. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1564. write_sequnlock(&state->seqlock);
  1565. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1566. }
  1567. }
  1568. /**
  1569. * nfs41_check_open_stateid - possibly free an open stateid
  1570. *
  1571. * @state: NFSv4 state for an inode
  1572. *
  1573. * Returns NFS_OK if recovery for this stateid is now finished.
  1574. * Otherwise a negative NFS4ERR value is returned.
  1575. */
  1576. static int nfs41_check_open_stateid(struct nfs4_state *state)
  1577. {
  1578. struct nfs_server *server = NFS_SERVER(state->inode);
  1579. nfs4_stateid *stateid = &state->open_stateid;
  1580. int status;
  1581. /* If a state reset has been done, test_stateid is unneeded */
  1582. if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
  1583. (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
  1584. (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
  1585. return -NFS4ERR_BAD_STATEID;
  1586. status = nfs41_test_stateid(server, stateid);
  1587. if (status != NFS_OK) {
  1588. /* Free the stateid unless the server explicitly
  1589. * informs us the stateid is unrecognized. */
  1590. if (status != -NFS4ERR_BAD_STATEID)
  1591. nfs41_free_stateid(server, stateid);
  1592. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1593. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1594. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1595. }
  1596. return status;
  1597. }
  1598. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1599. {
  1600. int status;
  1601. nfs41_clear_delegation_stateid(state);
  1602. status = nfs41_check_open_stateid(state);
  1603. if (status != NFS_OK)
  1604. status = nfs4_open_expired(sp, state);
  1605. return status;
  1606. }
  1607. #endif
  1608. /*
  1609. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1610. * fields corresponding to attributes that were used to store the verifier.
  1611. * Make sure we clobber those fields in the later setattr call
  1612. */
  1613. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1614. {
  1615. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1616. !(sattr->ia_valid & ATTR_ATIME_SET))
  1617. sattr->ia_valid |= ATTR_ATIME;
  1618. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1619. !(sattr->ia_valid & ATTR_MTIME_SET))
  1620. sattr->ia_valid |= ATTR_MTIME;
  1621. }
  1622. /*
  1623. * Returns a referenced nfs4_state
  1624. */
  1625. static int _nfs4_do_open(struct inode *dir,
  1626. struct dentry *dentry,
  1627. fmode_t fmode,
  1628. int flags,
  1629. struct iattr *sattr,
  1630. struct rpc_cred *cred,
  1631. struct nfs4_state **res,
  1632. struct nfs4_threshold **ctx_th)
  1633. {
  1634. struct nfs4_state_owner *sp;
  1635. struct nfs4_state *state = NULL;
  1636. struct nfs_server *server = NFS_SERVER(dir);
  1637. struct nfs4_opendata *opendata;
  1638. int status;
  1639. /* Protect against reboot recovery conflicts */
  1640. status = -ENOMEM;
  1641. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  1642. if (sp == NULL) {
  1643. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1644. goto out_err;
  1645. }
  1646. status = nfs4_recover_expired_lease(server);
  1647. if (status != 0)
  1648. goto err_put_state_owner;
  1649. if (dentry->d_inode != NULL)
  1650. nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
  1651. status = -ENOMEM;
  1652. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
  1653. if (opendata == NULL)
  1654. goto err_put_state_owner;
  1655. if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  1656. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  1657. if (!opendata->f_attr.mdsthreshold)
  1658. goto err_opendata_put;
  1659. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  1660. }
  1661. if (dentry->d_inode != NULL)
  1662. opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
  1663. status = _nfs4_proc_open(opendata);
  1664. if (status != 0)
  1665. goto err_opendata_put;
  1666. state = nfs4_opendata_to_nfs4_state(opendata);
  1667. status = PTR_ERR(state);
  1668. if (IS_ERR(state))
  1669. goto err_opendata_put;
  1670. if (server->caps & NFS_CAP_POSIX_LOCK)
  1671. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  1672. status = nfs4_opendata_access(cred, opendata, state, fmode);
  1673. if (status != 0)
  1674. goto err_opendata_put;
  1675. if (opendata->o_arg.open_flags & O_EXCL) {
  1676. nfs4_exclusive_attrset(opendata, sattr);
  1677. nfs_fattr_init(opendata->o_res.f_attr);
  1678. status = nfs4_do_setattr(state->inode, cred,
  1679. opendata->o_res.f_attr, sattr,
  1680. state);
  1681. if (status == 0)
  1682. nfs_setattr_update_inode(state->inode, sattr);
  1683. nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
  1684. }
  1685. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
  1686. *ctx_th = opendata->f_attr.mdsthreshold;
  1687. else
  1688. kfree(opendata->f_attr.mdsthreshold);
  1689. opendata->f_attr.mdsthreshold = NULL;
  1690. nfs4_opendata_put(opendata);
  1691. nfs4_put_state_owner(sp);
  1692. *res = state;
  1693. return 0;
  1694. err_opendata_put:
  1695. kfree(opendata->f_attr.mdsthreshold);
  1696. nfs4_opendata_put(opendata);
  1697. err_put_state_owner:
  1698. nfs4_put_state_owner(sp);
  1699. out_err:
  1700. *res = NULL;
  1701. return status;
  1702. }
  1703. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  1704. struct dentry *dentry,
  1705. fmode_t fmode,
  1706. int flags,
  1707. struct iattr *sattr,
  1708. struct rpc_cred *cred,
  1709. struct nfs4_threshold **ctx_th)
  1710. {
  1711. struct nfs4_exception exception = { };
  1712. struct nfs4_state *res;
  1713. int status;
  1714. fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
  1715. do {
  1716. status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
  1717. &res, ctx_th);
  1718. if (status == 0)
  1719. break;
  1720. /* NOTE: BAD_SEQID means the server and client disagree about the
  1721. * book-keeping w.r.t. state-changing operations
  1722. * (OPEN/CLOSE/LOCK/LOCKU...)
  1723. * It is actually a sign of a bug on the client or on the server.
  1724. *
  1725. * If we receive a BAD_SEQID error in the particular case of
  1726. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1727. * have unhashed the old state_owner for us, and that we can
  1728. * therefore safely retry using a new one. We should still warn
  1729. * the user though...
  1730. */
  1731. if (status == -NFS4ERR_BAD_SEQID) {
  1732. pr_warn_ratelimited("NFS: v4 server %s "
  1733. " returned a bad sequence-id error!\n",
  1734. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1735. exception.retry = 1;
  1736. continue;
  1737. }
  1738. /*
  1739. * BAD_STATEID on OPEN means that the server cancelled our
  1740. * state before it received the OPEN_CONFIRM.
  1741. * Recover by retrying the request as per the discussion
  1742. * on Page 181 of RFC3530.
  1743. */
  1744. if (status == -NFS4ERR_BAD_STATEID) {
  1745. exception.retry = 1;
  1746. continue;
  1747. }
  1748. if (status == -EAGAIN) {
  1749. /* We must have found a delegation */
  1750. exception.retry = 1;
  1751. continue;
  1752. }
  1753. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  1754. status, &exception));
  1755. } while (exception.retry);
  1756. return res;
  1757. }
  1758. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1759. struct nfs_fattr *fattr, struct iattr *sattr,
  1760. struct nfs4_state *state)
  1761. {
  1762. struct nfs_server *server = NFS_SERVER(inode);
  1763. struct nfs_setattrargs arg = {
  1764. .fh = NFS_FH(inode),
  1765. .iap = sattr,
  1766. .server = server,
  1767. .bitmask = server->attr_bitmask,
  1768. };
  1769. struct nfs_setattrres res = {
  1770. .fattr = fattr,
  1771. .server = server,
  1772. };
  1773. struct rpc_message msg = {
  1774. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1775. .rpc_argp = &arg,
  1776. .rpc_resp = &res,
  1777. .rpc_cred = cred,
  1778. };
  1779. unsigned long timestamp = jiffies;
  1780. int status;
  1781. nfs_fattr_init(fattr);
  1782. if (state != NULL) {
  1783. struct nfs_lockowner lockowner = {
  1784. .l_owner = current->files,
  1785. .l_pid = current->tgid,
  1786. };
  1787. nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
  1788. &lockowner);
  1789. } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
  1790. FMODE_WRITE)) {
  1791. /* Use that stateid */
  1792. } else
  1793. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  1794. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  1795. if (status == 0 && state != NULL)
  1796. renew_lease(server, timestamp);
  1797. return status;
  1798. }
  1799. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1800. struct nfs_fattr *fattr, struct iattr *sattr,
  1801. struct nfs4_state *state)
  1802. {
  1803. struct nfs_server *server = NFS_SERVER(inode);
  1804. struct nfs4_exception exception = {
  1805. .state = state,
  1806. .inode = inode,
  1807. };
  1808. int err;
  1809. do {
  1810. err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
  1811. switch (err) {
  1812. case -NFS4ERR_OPENMODE:
  1813. if (state && !(state->state & FMODE_WRITE)) {
  1814. err = -EBADF;
  1815. if (sattr->ia_valid & ATTR_OPEN)
  1816. err = -EACCES;
  1817. goto out;
  1818. }
  1819. }
  1820. err = nfs4_handle_exception(server, err, &exception);
  1821. } while (exception.retry);
  1822. out:
  1823. return err;
  1824. }
  1825. struct nfs4_closedata {
  1826. struct inode *inode;
  1827. struct nfs4_state *state;
  1828. struct nfs_closeargs arg;
  1829. struct nfs_closeres res;
  1830. struct nfs_fattr fattr;
  1831. unsigned long timestamp;
  1832. bool roc;
  1833. u32 roc_barrier;
  1834. };
  1835. static void nfs4_free_closedata(void *data)
  1836. {
  1837. struct nfs4_closedata *calldata = data;
  1838. struct nfs4_state_owner *sp = calldata->state->owner;
  1839. struct super_block *sb = calldata->state->inode->i_sb;
  1840. if (calldata->roc)
  1841. pnfs_roc_release(calldata->state->inode);
  1842. nfs4_put_open_state(calldata->state);
  1843. nfs_free_seqid(calldata->arg.seqid);
  1844. nfs4_put_state_owner(sp);
  1845. nfs_sb_deactive_async(sb);
  1846. kfree(calldata);
  1847. }
  1848. static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
  1849. fmode_t fmode)
  1850. {
  1851. spin_lock(&state->owner->so_lock);
  1852. if (!(fmode & FMODE_READ))
  1853. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1854. if (!(fmode & FMODE_WRITE))
  1855. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1856. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1857. spin_unlock(&state->owner->so_lock);
  1858. }
  1859. static void nfs4_close_done(struct rpc_task *task, void *data)
  1860. {
  1861. struct nfs4_closedata *calldata = data;
  1862. struct nfs4_state *state = calldata->state;
  1863. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1864. dprintk("%s: begin!\n", __func__);
  1865. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  1866. return;
  1867. /* hmm. we are done with the inode, and in the process of freeing
  1868. * the state_owner. we keep this around to process errors
  1869. */
  1870. switch (task->tk_status) {
  1871. case 0:
  1872. if (calldata->roc)
  1873. pnfs_roc_set_barrier(state->inode,
  1874. calldata->roc_barrier);
  1875. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1876. renew_lease(server, calldata->timestamp);
  1877. nfs4_close_clear_stateid_flags(state,
  1878. calldata->arg.fmode);
  1879. break;
  1880. case -NFS4ERR_STALE_STATEID:
  1881. case -NFS4ERR_OLD_STATEID:
  1882. case -NFS4ERR_BAD_STATEID:
  1883. case -NFS4ERR_EXPIRED:
  1884. if (calldata->arg.fmode == 0)
  1885. break;
  1886. default:
  1887. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  1888. rpc_restart_call_prepare(task);
  1889. }
  1890. nfs_release_seqid(calldata->arg.seqid);
  1891. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1892. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  1893. }
  1894. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1895. {
  1896. struct nfs4_closedata *calldata = data;
  1897. struct nfs4_state *state = calldata->state;
  1898. struct inode *inode = calldata->inode;
  1899. int call_close = 0;
  1900. dprintk("%s: begin!\n", __func__);
  1901. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1902. return;
  1903. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1904. calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
  1905. spin_lock(&state->owner->so_lock);
  1906. /* Calculate the change in open mode */
  1907. if (state->n_rdwr == 0) {
  1908. if (state->n_rdonly == 0) {
  1909. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  1910. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  1911. calldata->arg.fmode &= ~FMODE_READ;
  1912. }
  1913. if (state->n_wronly == 0) {
  1914. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  1915. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  1916. calldata->arg.fmode &= ~FMODE_WRITE;
  1917. }
  1918. }
  1919. spin_unlock(&state->owner->so_lock);
  1920. if (!call_close) {
  1921. /* Note: exit _without_ calling nfs4_close_done */
  1922. task->tk_action = NULL;
  1923. nfs4_sequence_done(task, &calldata->res.seq_res);
  1924. goto out;
  1925. }
  1926. if (calldata->arg.fmode == 0) {
  1927. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  1928. if (calldata->roc &&
  1929. pnfs_roc_drain(inode, &calldata->roc_barrier, task))
  1930. goto out;
  1931. }
  1932. nfs_fattr_init(calldata->res.fattr);
  1933. calldata->timestamp = jiffies;
  1934. if (nfs4_setup_sequence(NFS_SERVER(inode),
  1935. &calldata->arg.seq_args,
  1936. &calldata->res.seq_res,
  1937. task) != 0)
  1938. nfs_release_seqid(calldata->arg.seqid);
  1939. out:
  1940. dprintk("%s: done!\n", __func__);
  1941. }
  1942. static const struct rpc_call_ops nfs4_close_ops = {
  1943. .rpc_call_prepare = nfs4_close_prepare,
  1944. .rpc_call_done = nfs4_close_done,
  1945. .rpc_release = nfs4_free_closedata,
  1946. };
  1947. /*
  1948. * It is possible for data to be read/written from a mem-mapped file
  1949. * after the sys_close call (which hits the vfs layer as a flush).
  1950. * This means that we can't safely call nfsv4 close on a file until
  1951. * the inode is cleared. This in turn means that we are not good
  1952. * NFSv4 citizens - we do not indicate to the server to update the file's
  1953. * share state even when we are done with one of the three share
  1954. * stateid's in the inode.
  1955. *
  1956. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1957. */
  1958. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  1959. {
  1960. struct nfs_server *server = NFS_SERVER(state->inode);
  1961. struct nfs4_closedata *calldata;
  1962. struct nfs4_state_owner *sp = state->owner;
  1963. struct rpc_task *task;
  1964. struct rpc_message msg = {
  1965. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1966. .rpc_cred = state->owner->so_cred,
  1967. };
  1968. struct rpc_task_setup task_setup_data = {
  1969. .rpc_client = server->client,
  1970. .rpc_message = &msg,
  1971. .callback_ops = &nfs4_close_ops,
  1972. .workqueue = nfsiod_workqueue,
  1973. .flags = RPC_TASK_ASYNC,
  1974. };
  1975. int status = -ENOMEM;
  1976. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  1977. if (calldata == NULL)
  1978. goto out;
  1979. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
  1980. calldata->inode = state->inode;
  1981. calldata->state = state;
  1982. calldata->arg.fh = NFS_FH(state->inode);
  1983. calldata->arg.stateid = &state->open_stateid;
  1984. /* Serialization for the sequence id */
  1985. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
  1986. if (calldata->arg.seqid == NULL)
  1987. goto out_free_calldata;
  1988. calldata->arg.fmode = 0;
  1989. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1990. calldata->res.fattr = &calldata->fattr;
  1991. calldata->res.seqid = calldata->arg.seqid;
  1992. calldata->res.server = server;
  1993. calldata->roc = pnfs_roc(state->inode);
  1994. nfs_sb_active(calldata->inode->i_sb);
  1995. msg.rpc_argp = &calldata->arg;
  1996. msg.rpc_resp = &calldata->res;
  1997. task_setup_data.callback_data = calldata;
  1998. task = rpc_run_task(&task_setup_data);
  1999. if (IS_ERR(task))
  2000. return PTR_ERR(task);
  2001. status = 0;
  2002. if (wait)
  2003. status = rpc_wait_for_completion_task(task);
  2004. rpc_put_task(task);
  2005. return status;
  2006. out_free_calldata:
  2007. kfree(calldata);
  2008. out:
  2009. nfs4_put_open_state(state);
  2010. nfs4_put_state_owner(sp);
  2011. return status;
  2012. }
  2013. static struct inode *
  2014. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
  2015. {
  2016. struct nfs4_state *state;
  2017. /* Protect against concurrent sillydeletes */
  2018. state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
  2019. ctx->cred, &ctx->mdsthreshold);
  2020. if (IS_ERR(state))
  2021. return ERR_CAST(state);
  2022. ctx->state = state;
  2023. return igrab(state->inode);
  2024. }
  2025. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  2026. {
  2027. if (ctx->state == NULL)
  2028. return;
  2029. if (is_sync)
  2030. nfs4_close_sync(ctx->state, ctx->mode);
  2031. else
  2032. nfs4_close_state(ctx->state, ctx->mode);
  2033. }
  2034. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2035. {
  2036. struct nfs4_server_caps_arg args = {
  2037. .fhandle = fhandle,
  2038. };
  2039. struct nfs4_server_caps_res res = {};
  2040. struct rpc_message msg = {
  2041. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  2042. .rpc_argp = &args,
  2043. .rpc_resp = &res,
  2044. };
  2045. int status;
  2046. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2047. if (status == 0) {
  2048. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  2049. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  2050. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  2051. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  2052. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  2053. NFS_CAP_CTIME|NFS_CAP_MTIME);
  2054. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  2055. server->caps |= NFS_CAP_ACLS;
  2056. if (res.has_links != 0)
  2057. server->caps |= NFS_CAP_HARDLINKS;
  2058. if (res.has_symlinks != 0)
  2059. server->caps |= NFS_CAP_SYMLINKS;
  2060. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  2061. server->caps |= NFS_CAP_FILEID;
  2062. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  2063. server->caps |= NFS_CAP_MODE;
  2064. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  2065. server->caps |= NFS_CAP_NLINK;
  2066. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  2067. server->caps |= NFS_CAP_OWNER;
  2068. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  2069. server->caps |= NFS_CAP_OWNER_GROUP;
  2070. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  2071. server->caps |= NFS_CAP_ATIME;
  2072. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  2073. server->caps |= NFS_CAP_CTIME;
  2074. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  2075. server->caps |= NFS_CAP_MTIME;
  2076. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  2077. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  2078. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  2079. server->acl_bitmask = res.acl_bitmask;
  2080. server->fh_expire_type = res.fh_expire_type;
  2081. }
  2082. return status;
  2083. }
  2084. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2085. {
  2086. struct nfs4_exception exception = { };
  2087. int err;
  2088. do {
  2089. err = nfs4_handle_exception(server,
  2090. _nfs4_server_capabilities(server, fhandle),
  2091. &exception);
  2092. } while (exception.retry);
  2093. return err;
  2094. }
  2095. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2096. struct nfs_fsinfo *info)
  2097. {
  2098. struct nfs4_lookup_root_arg args = {
  2099. .bitmask = nfs4_fattr_bitmap,
  2100. };
  2101. struct nfs4_lookup_res res = {
  2102. .server = server,
  2103. .fattr = info->fattr,
  2104. .fh = fhandle,
  2105. };
  2106. struct rpc_message msg = {
  2107. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  2108. .rpc_argp = &args,
  2109. .rpc_resp = &res,
  2110. };
  2111. nfs_fattr_init(info->fattr);
  2112. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2113. }
  2114. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2115. struct nfs_fsinfo *info)
  2116. {
  2117. struct nfs4_exception exception = { };
  2118. int err;
  2119. do {
  2120. err = _nfs4_lookup_root(server, fhandle, info);
  2121. switch (err) {
  2122. case 0:
  2123. case -NFS4ERR_WRONGSEC:
  2124. goto out;
  2125. default:
  2126. err = nfs4_handle_exception(server, err, &exception);
  2127. }
  2128. } while (exception.retry);
  2129. out:
  2130. return err;
  2131. }
  2132. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2133. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  2134. {
  2135. struct rpc_auth *auth;
  2136. int ret;
  2137. auth = rpcauth_create(flavor, server->client);
  2138. if (IS_ERR(auth)) {
  2139. ret = -EIO;
  2140. goto out;
  2141. }
  2142. ret = nfs4_lookup_root(server, fhandle, info);
  2143. out:
  2144. return ret;
  2145. }
  2146. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2147. struct nfs_fsinfo *info)
  2148. {
  2149. int i, len, status = 0;
  2150. rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
  2151. len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
  2152. if (len < 0)
  2153. return len;
  2154. for (i = 0; i < len; i++) {
  2155. /* AUTH_UNIX is the default flavor if none was specified,
  2156. * thus has already been tried. */
  2157. if (flav_array[i] == RPC_AUTH_UNIX)
  2158. continue;
  2159. status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
  2160. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  2161. continue;
  2162. break;
  2163. }
  2164. /*
  2165. * -EACCESS could mean that the user doesn't have correct permissions
  2166. * to access the mount. It could also mean that we tried to mount
  2167. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  2168. * existing mount programs don't handle -EACCES very well so it should
  2169. * be mapped to -EPERM instead.
  2170. */
  2171. if (status == -EACCES)
  2172. status = -EPERM;
  2173. return status;
  2174. }
  2175. /*
  2176. * get the file handle for the "/" directory on the server
  2177. */
  2178. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  2179. struct nfs_fsinfo *info)
  2180. {
  2181. int minor_version = server->nfs_client->cl_minorversion;
  2182. int status = nfs4_lookup_root(server, fhandle, info);
  2183. if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
  2184. /*
  2185. * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
  2186. * by nfs4_map_errors() as this function exits.
  2187. */
  2188. status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
  2189. if (status == 0)
  2190. status = nfs4_server_capabilities(server, fhandle);
  2191. if (status == 0)
  2192. status = nfs4_do_fsinfo(server, fhandle, info);
  2193. return nfs4_map_errors(status);
  2194. }
  2195. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  2196. struct nfs_fsinfo *info)
  2197. {
  2198. int error;
  2199. struct nfs_fattr *fattr = info->fattr;
  2200. error = nfs4_server_capabilities(server, mntfh);
  2201. if (error < 0) {
  2202. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  2203. return error;
  2204. }
  2205. error = nfs4_proc_getattr(server, mntfh, fattr);
  2206. if (error < 0) {
  2207. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  2208. return error;
  2209. }
  2210. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  2211. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  2212. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  2213. return error;
  2214. }
  2215. /*
  2216. * Get locations and (maybe) other attributes of a referral.
  2217. * Note that we'll actually follow the referral later when
  2218. * we detect fsid mismatch in inode revalidation
  2219. */
  2220. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  2221. const struct qstr *name, struct nfs_fattr *fattr,
  2222. struct nfs_fh *fhandle)
  2223. {
  2224. int status = -ENOMEM;
  2225. struct page *page = NULL;
  2226. struct nfs4_fs_locations *locations = NULL;
  2227. page = alloc_page(GFP_KERNEL);
  2228. if (page == NULL)
  2229. goto out;
  2230. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2231. if (locations == NULL)
  2232. goto out;
  2233. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  2234. if (status != 0)
  2235. goto out;
  2236. /* Make sure server returned a different fsid for the referral */
  2237. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  2238. dprintk("%s: server did not return a different fsid for"
  2239. " a referral at %s\n", __func__, name->name);
  2240. status = -EIO;
  2241. goto out;
  2242. }
  2243. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  2244. nfs_fixup_referral_attributes(&locations->fattr);
  2245. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  2246. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  2247. memset(fhandle, 0, sizeof(struct nfs_fh));
  2248. out:
  2249. if (page)
  2250. __free_page(page);
  2251. kfree(locations);
  2252. return status;
  2253. }
  2254. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2255. {
  2256. struct nfs4_getattr_arg args = {
  2257. .fh = fhandle,
  2258. .bitmask = server->attr_bitmask,
  2259. };
  2260. struct nfs4_getattr_res res = {
  2261. .fattr = fattr,
  2262. .server = server,
  2263. };
  2264. struct rpc_message msg = {
  2265. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  2266. .rpc_argp = &args,
  2267. .rpc_resp = &res,
  2268. };
  2269. nfs_fattr_init(fattr);
  2270. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2271. }
  2272. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2273. {
  2274. struct nfs4_exception exception = { };
  2275. int err;
  2276. do {
  2277. err = nfs4_handle_exception(server,
  2278. _nfs4_proc_getattr(server, fhandle, fattr),
  2279. &exception);
  2280. } while (exception.retry);
  2281. return err;
  2282. }
  2283. /*
  2284. * The file is not closed if it is opened due to the a request to change
  2285. * the size of the file. The open call will not be needed once the
  2286. * VFS layer lookup-intents are implemented.
  2287. *
  2288. * Close is called when the inode is destroyed.
  2289. * If we haven't opened the file for O_WRONLY, we
  2290. * need to in the size_change case to obtain a stateid.
  2291. *
  2292. * Got race?
  2293. * Because OPEN is always done by name in nfsv4, it is
  2294. * possible that we opened a different file by the same
  2295. * name. We can recognize this race condition, but we
  2296. * can't do anything about it besides returning an error.
  2297. *
  2298. * This will be fixed with VFS changes (lookup-intent).
  2299. */
  2300. static int
  2301. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  2302. struct iattr *sattr)
  2303. {
  2304. struct inode *inode = dentry->d_inode;
  2305. struct rpc_cred *cred = NULL;
  2306. struct nfs4_state *state = NULL;
  2307. int status;
  2308. if (pnfs_ld_layoutret_on_setattr(inode))
  2309. pnfs_return_layout(inode);
  2310. nfs_fattr_init(fattr);
  2311. /* Deal with open(O_TRUNC) */
  2312. if (sattr->ia_valid & ATTR_OPEN)
  2313. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
  2314. /* Optimization: if the end result is no change, don't RPC */
  2315. if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
  2316. return 0;
  2317. /* Search for an existing open(O_WRITE) file */
  2318. if (sattr->ia_valid & ATTR_FILE) {
  2319. struct nfs_open_context *ctx;
  2320. ctx = nfs_file_open_context(sattr->ia_file);
  2321. if (ctx) {
  2322. cred = ctx->cred;
  2323. state = ctx->state;
  2324. }
  2325. }
  2326. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2327. if (status == 0)
  2328. nfs_setattr_update_inode(inode, sattr);
  2329. return status;
  2330. }
  2331. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  2332. const struct qstr *name, struct nfs_fh *fhandle,
  2333. struct nfs_fattr *fattr)
  2334. {
  2335. struct nfs_server *server = NFS_SERVER(dir);
  2336. int status;
  2337. struct nfs4_lookup_arg args = {
  2338. .bitmask = server->attr_bitmask,
  2339. .dir_fh = NFS_FH(dir),
  2340. .name = name,
  2341. };
  2342. struct nfs4_lookup_res res = {
  2343. .server = server,
  2344. .fattr = fattr,
  2345. .fh = fhandle,
  2346. };
  2347. struct rpc_message msg = {
  2348. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2349. .rpc_argp = &args,
  2350. .rpc_resp = &res,
  2351. };
  2352. nfs_fattr_init(fattr);
  2353. dprintk("NFS call lookup %s\n", name->name);
  2354. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  2355. dprintk("NFS reply lookup: %d\n", status);
  2356. return status;
  2357. }
  2358. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  2359. {
  2360. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  2361. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  2362. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  2363. fattr->nlink = 2;
  2364. }
  2365. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  2366. struct qstr *name, struct nfs_fh *fhandle,
  2367. struct nfs_fattr *fattr)
  2368. {
  2369. struct nfs4_exception exception = { };
  2370. struct rpc_clnt *client = *clnt;
  2371. int err;
  2372. do {
  2373. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
  2374. switch (err) {
  2375. case -NFS4ERR_BADNAME:
  2376. err = -ENOENT;
  2377. goto out;
  2378. case -NFS4ERR_MOVED:
  2379. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  2380. goto out;
  2381. case -NFS4ERR_WRONGSEC:
  2382. err = -EPERM;
  2383. if (client != *clnt)
  2384. goto out;
  2385. client = nfs4_create_sec_client(client, dir, name);
  2386. if (IS_ERR(client))
  2387. return PTR_ERR(client);
  2388. exception.retry = 1;
  2389. break;
  2390. default:
  2391. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  2392. }
  2393. } while (exception.retry);
  2394. out:
  2395. if (err == 0)
  2396. *clnt = client;
  2397. else if (client != *clnt)
  2398. rpc_shutdown_client(client);
  2399. return err;
  2400. }
  2401. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
  2402. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2403. {
  2404. int status;
  2405. struct rpc_clnt *client = NFS_CLIENT(dir);
  2406. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2407. if (client != NFS_CLIENT(dir)) {
  2408. rpc_shutdown_client(client);
  2409. nfs_fixup_secinfo_attributes(fattr);
  2410. }
  2411. return status;
  2412. }
  2413. struct rpc_clnt *
  2414. nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
  2415. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2416. {
  2417. int status;
  2418. struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
  2419. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2420. if (status < 0) {
  2421. rpc_shutdown_client(client);
  2422. return ERR_PTR(status);
  2423. }
  2424. return client;
  2425. }
  2426. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2427. {
  2428. struct nfs_server *server = NFS_SERVER(inode);
  2429. struct nfs4_accessargs args = {
  2430. .fh = NFS_FH(inode),
  2431. .bitmask = server->cache_consistency_bitmask,
  2432. };
  2433. struct nfs4_accessres res = {
  2434. .server = server,
  2435. };
  2436. struct rpc_message msg = {
  2437. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2438. .rpc_argp = &args,
  2439. .rpc_resp = &res,
  2440. .rpc_cred = entry->cred,
  2441. };
  2442. int mode = entry->mask;
  2443. int status;
  2444. /*
  2445. * Determine which access bits we want to ask for...
  2446. */
  2447. if (mode & MAY_READ)
  2448. args.access |= NFS4_ACCESS_READ;
  2449. if (S_ISDIR(inode->i_mode)) {
  2450. if (mode & MAY_WRITE)
  2451. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2452. if (mode & MAY_EXEC)
  2453. args.access |= NFS4_ACCESS_LOOKUP;
  2454. } else {
  2455. if (mode & MAY_WRITE)
  2456. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2457. if (mode & MAY_EXEC)
  2458. args.access |= NFS4_ACCESS_EXECUTE;
  2459. }
  2460. res.fattr = nfs_alloc_fattr();
  2461. if (res.fattr == NULL)
  2462. return -ENOMEM;
  2463. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2464. if (!status) {
  2465. nfs_access_set_mask(entry, res.access);
  2466. nfs_refresh_inode(inode, res.fattr);
  2467. }
  2468. nfs_free_fattr(res.fattr);
  2469. return status;
  2470. }
  2471. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2472. {
  2473. struct nfs4_exception exception = { };
  2474. int err;
  2475. do {
  2476. err = nfs4_handle_exception(NFS_SERVER(inode),
  2477. _nfs4_proc_access(inode, entry),
  2478. &exception);
  2479. } while (exception.retry);
  2480. return err;
  2481. }
  2482. /*
  2483. * TODO: For the time being, we don't try to get any attributes
  2484. * along with any of the zero-copy operations READ, READDIR,
  2485. * READLINK, WRITE.
  2486. *
  2487. * In the case of the first three, we want to put the GETATTR
  2488. * after the read-type operation -- this is because it is hard
  2489. * to predict the length of a GETATTR response in v4, and thus
  2490. * align the READ data correctly. This means that the GETATTR
  2491. * may end up partially falling into the page cache, and we should
  2492. * shift it into the 'tail' of the xdr_buf before processing.
  2493. * To do this efficiently, we need to know the total length
  2494. * of data received, which doesn't seem to be available outside
  2495. * of the RPC layer.
  2496. *
  2497. * In the case of WRITE, we also want to put the GETATTR after
  2498. * the operation -- in this case because we want to make sure
  2499. * we get the post-operation mtime and size.
  2500. *
  2501. * Both of these changes to the XDR layer would in fact be quite
  2502. * minor, but I decided to leave them for a subsequent patch.
  2503. */
  2504. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2505. unsigned int pgbase, unsigned int pglen)
  2506. {
  2507. struct nfs4_readlink args = {
  2508. .fh = NFS_FH(inode),
  2509. .pgbase = pgbase,
  2510. .pglen = pglen,
  2511. .pages = &page,
  2512. };
  2513. struct nfs4_readlink_res res;
  2514. struct rpc_message msg = {
  2515. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2516. .rpc_argp = &args,
  2517. .rpc_resp = &res,
  2518. };
  2519. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  2520. }
  2521. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2522. unsigned int pgbase, unsigned int pglen)
  2523. {
  2524. struct nfs4_exception exception = { };
  2525. int err;
  2526. do {
  2527. err = nfs4_handle_exception(NFS_SERVER(inode),
  2528. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2529. &exception);
  2530. } while (exception.retry);
  2531. return err;
  2532. }
  2533. /*
  2534. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  2535. */
  2536. static int
  2537. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2538. int flags)
  2539. {
  2540. struct nfs_open_context *ctx;
  2541. struct nfs4_state *state;
  2542. int status = 0;
  2543. ctx = alloc_nfs_open_context(dentry, FMODE_READ);
  2544. if (IS_ERR(ctx))
  2545. return PTR_ERR(ctx);
  2546. sattr->ia_mode &= ~current_umask();
  2547. state = nfs4_do_open(dir, dentry, ctx->mode,
  2548. flags, sattr, ctx->cred,
  2549. &ctx->mdsthreshold);
  2550. d_drop(dentry);
  2551. if (IS_ERR(state)) {
  2552. status = PTR_ERR(state);
  2553. goto out;
  2554. }
  2555. d_add(dentry, igrab(state->inode));
  2556. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2557. ctx->state = state;
  2558. out:
  2559. put_nfs_open_context(ctx);
  2560. return status;
  2561. }
  2562. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2563. {
  2564. struct nfs_server *server = NFS_SERVER(dir);
  2565. struct nfs_removeargs args = {
  2566. .fh = NFS_FH(dir),
  2567. .name = *name,
  2568. };
  2569. struct nfs_removeres res = {
  2570. .server = server,
  2571. };
  2572. struct rpc_message msg = {
  2573. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2574. .rpc_argp = &args,
  2575. .rpc_resp = &res,
  2576. };
  2577. int status;
  2578. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  2579. if (status == 0)
  2580. update_changeattr(dir, &res.cinfo);
  2581. return status;
  2582. }
  2583. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2584. {
  2585. struct nfs4_exception exception = { };
  2586. int err;
  2587. do {
  2588. err = nfs4_handle_exception(NFS_SERVER(dir),
  2589. _nfs4_proc_remove(dir, name),
  2590. &exception);
  2591. } while (exception.retry);
  2592. return err;
  2593. }
  2594. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2595. {
  2596. struct nfs_server *server = NFS_SERVER(dir);
  2597. struct nfs_removeargs *args = msg->rpc_argp;
  2598. struct nfs_removeres *res = msg->rpc_resp;
  2599. res->server = server;
  2600. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2601. nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
  2602. }
  2603. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  2604. {
  2605. nfs4_setup_sequence(NFS_SERVER(data->dir),
  2606. &data->args.seq_args,
  2607. &data->res.seq_res,
  2608. task);
  2609. }
  2610. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2611. {
  2612. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2613. if (!nfs4_sequence_done(task, &res->seq_res))
  2614. return 0;
  2615. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2616. return 0;
  2617. update_changeattr(dir, &res->cinfo);
  2618. return 1;
  2619. }
  2620. static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  2621. {
  2622. struct nfs_server *server = NFS_SERVER(dir);
  2623. struct nfs_renameargs *arg = msg->rpc_argp;
  2624. struct nfs_renameres *res = msg->rpc_resp;
  2625. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  2626. res->server = server;
  2627. nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
  2628. }
  2629. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  2630. {
  2631. nfs4_setup_sequence(NFS_SERVER(data->old_dir),
  2632. &data->args.seq_args,
  2633. &data->res.seq_res,
  2634. task);
  2635. }
  2636. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  2637. struct inode *new_dir)
  2638. {
  2639. struct nfs_renameres *res = task->tk_msg.rpc_resp;
  2640. if (!nfs4_sequence_done(task, &res->seq_res))
  2641. return 0;
  2642. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2643. return 0;
  2644. update_changeattr(old_dir, &res->old_cinfo);
  2645. update_changeattr(new_dir, &res->new_cinfo);
  2646. return 1;
  2647. }
  2648. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2649. struct inode *new_dir, struct qstr *new_name)
  2650. {
  2651. struct nfs_server *server = NFS_SERVER(old_dir);
  2652. struct nfs_renameargs arg = {
  2653. .old_dir = NFS_FH(old_dir),
  2654. .new_dir = NFS_FH(new_dir),
  2655. .old_name = old_name,
  2656. .new_name = new_name,
  2657. };
  2658. struct nfs_renameres res = {
  2659. .server = server,
  2660. };
  2661. struct rpc_message msg = {
  2662. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2663. .rpc_argp = &arg,
  2664. .rpc_resp = &res,
  2665. };
  2666. int status = -ENOMEM;
  2667. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2668. if (!status) {
  2669. update_changeattr(old_dir, &res.old_cinfo);
  2670. update_changeattr(new_dir, &res.new_cinfo);
  2671. }
  2672. return status;
  2673. }
  2674. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2675. struct inode *new_dir, struct qstr *new_name)
  2676. {
  2677. struct nfs4_exception exception = { };
  2678. int err;
  2679. do {
  2680. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2681. _nfs4_proc_rename(old_dir, old_name,
  2682. new_dir, new_name),
  2683. &exception);
  2684. } while (exception.retry);
  2685. return err;
  2686. }
  2687. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2688. {
  2689. struct nfs_server *server = NFS_SERVER(inode);
  2690. struct nfs4_link_arg arg = {
  2691. .fh = NFS_FH(inode),
  2692. .dir_fh = NFS_FH(dir),
  2693. .name = name,
  2694. .bitmask = server->attr_bitmask,
  2695. };
  2696. struct nfs4_link_res res = {
  2697. .server = server,
  2698. };
  2699. struct rpc_message msg = {
  2700. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2701. .rpc_argp = &arg,
  2702. .rpc_resp = &res,
  2703. };
  2704. int status = -ENOMEM;
  2705. res.fattr = nfs_alloc_fattr();
  2706. if (res.fattr == NULL)
  2707. goto out;
  2708. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2709. if (!status) {
  2710. update_changeattr(dir, &res.cinfo);
  2711. nfs_post_op_update_inode(inode, res.fattr);
  2712. }
  2713. out:
  2714. nfs_free_fattr(res.fattr);
  2715. return status;
  2716. }
  2717. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2718. {
  2719. struct nfs4_exception exception = { };
  2720. int err;
  2721. do {
  2722. err = nfs4_handle_exception(NFS_SERVER(inode),
  2723. _nfs4_proc_link(inode, dir, name),
  2724. &exception);
  2725. } while (exception.retry);
  2726. return err;
  2727. }
  2728. struct nfs4_createdata {
  2729. struct rpc_message msg;
  2730. struct nfs4_create_arg arg;
  2731. struct nfs4_create_res res;
  2732. struct nfs_fh fh;
  2733. struct nfs_fattr fattr;
  2734. };
  2735. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2736. struct qstr *name, struct iattr *sattr, u32 ftype)
  2737. {
  2738. struct nfs4_createdata *data;
  2739. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2740. if (data != NULL) {
  2741. struct nfs_server *server = NFS_SERVER(dir);
  2742. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2743. data->msg.rpc_argp = &data->arg;
  2744. data->msg.rpc_resp = &data->res;
  2745. data->arg.dir_fh = NFS_FH(dir);
  2746. data->arg.server = server;
  2747. data->arg.name = name;
  2748. data->arg.attrs = sattr;
  2749. data->arg.ftype = ftype;
  2750. data->arg.bitmask = server->attr_bitmask;
  2751. data->res.server = server;
  2752. data->res.fh = &data->fh;
  2753. data->res.fattr = &data->fattr;
  2754. nfs_fattr_init(data->res.fattr);
  2755. }
  2756. return data;
  2757. }
  2758. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2759. {
  2760. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  2761. &data->arg.seq_args, &data->res.seq_res, 1);
  2762. if (status == 0) {
  2763. update_changeattr(dir, &data->res.dir_cinfo);
  2764. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2765. }
  2766. return status;
  2767. }
  2768. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2769. {
  2770. kfree(data);
  2771. }
  2772. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2773. struct page *page, unsigned int len, struct iattr *sattr)
  2774. {
  2775. struct nfs4_createdata *data;
  2776. int status = -ENAMETOOLONG;
  2777. if (len > NFS4_MAXPATHLEN)
  2778. goto out;
  2779. status = -ENOMEM;
  2780. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2781. if (data == NULL)
  2782. goto out;
  2783. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2784. data->arg.u.symlink.pages = &page;
  2785. data->arg.u.symlink.len = len;
  2786. status = nfs4_do_create(dir, dentry, data);
  2787. nfs4_free_createdata(data);
  2788. out:
  2789. return status;
  2790. }
  2791. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2792. struct page *page, unsigned int len, struct iattr *sattr)
  2793. {
  2794. struct nfs4_exception exception = { };
  2795. int err;
  2796. do {
  2797. err = nfs4_handle_exception(NFS_SERVER(dir),
  2798. _nfs4_proc_symlink(dir, dentry, page,
  2799. len, sattr),
  2800. &exception);
  2801. } while (exception.retry);
  2802. return err;
  2803. }
  2804. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2805. struct iattr *sattr)
  2806. {
  2807. struct nfs4_createdata *data;
  2808. int status = -ENOMEM;
  2809. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2810. if (data == NULL)
  2811. goto out;
  2812. status = nfs4_do_create(dir, dentry, data);
  2813. nfs4_free_createdata(data);
  2814. out:
  2815. return status;
  2816. }
  2817. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2818. struct iattr *sattr)
  2819. {
  2820. struct nfs4_exception exception = { };
  2821. int err;
  2822. sattr->ia_mode &= ~current_umask();
  2823. do {
  2824. err = nfs4_handle_exception(NFS_SERVER(dir),
  2825. _nfs4_proc_mkdir(dir, dentry, sattr),
  2826. &exception);
  2827. } while (exception.retry);
  2828. return err;
  2829. }
  2830. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2831. u64 cookie, struct page **pages, unsigned int count, int plus)
  2832. {
  2833. struct inode *dir = dentry->d_inode;
  2834. struct nfs4_readdir_arg args = {
  2835. .fh = NFS_FH(dir),
  2836. .pages = pages,
  2837. .pgbase = 0,
  2838. .count = count,
  2839. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2840. .plus = plus,
  2841. };
  2842. struct nfs4_readdir_res res;
  2843. struct rpc_message msg = {
  2844. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2845. .rpc_argp = &args,
  2846. .rpc_resp = &res,
  2847. .rpc_cred = cred,
  2848. };
  2849. int status;
  2850. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2851. dentry->d_parent->d_name.name,
  2852. dentry->d_name.name,
  2853. (unsigned long long)cookie);
  2854. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  2855. res.pgbase = args.pgbase;
  2856. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  2857. if (status >= 0) {
  2858. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  2859. status += args.pgbase;
  2860. }
  2861. nfs_invalidate_atime(dir);
  2862. dprintk("%s: returns %d\n", __func__, status);
  2863. return status;
  2864. }
  2865. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2866. u64 cookie, struct page **pages, unsigned int count, int plus)
  2867. {
  2868. struct nfs4_exception exception = { };
  2869. int err;
  2870. do {
  2871. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2872. _nfs4_proc_readdir(dentry, cred, cookie,
  2873. pages, count, plus),
  2874. &exception);
  2875. } while (exception.retry);
  2876. return err;
  2877. }
  2878. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2879. struct iattr *sattr, dev_t rdev)
  2880. {
  2881. struct nfs4_createdata *data;
  2882. int mode = sattr->ia_mode;
  2883. int status = -ENOMEM;
  2884. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2885. if (data == NULL)
  2886. goto out;
  2887. if (S_ISFIFO(mode))
  2888. data->arg.ftype = NF4FIFO;
  2889. else if (S_ISBLK(mode)) {
  2890. data->arg.ftype = NF4BLK;
  2891. data->arg.u.device.specdata1 = MAJOR(rdev);
  2892. data->arg.u.device.specdata2 = MINOR(rdev);
  2893. }
  2894. else if (S_ISCHR(mode)) {
  2895. data->arg.ftype = NF4CHR;
  2896. data->arg.u.device.specdata1 = MAJOR(rdev);
  2897. data->arg.u.device.specdata2 = MINOR(rdev);
  2898. } else if (!S_ISSOCK(mode)) {
  2899. status = -EINVAL;
  2900. goto out_free;
  2901. }
  2902. status = nfs4_do_create(dir, dentry, data);
  2903. out_free:
  2904. nfs4_free_createdata(data);
  2905. out:
  2906. return status;
  2907. }
  2908. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2909. struct iattr *sattr, dev_t rdev)
  2910. {
  2911. struct nfs4_exception exception = { };
  2912. int err;
  2913. sattr->ia_mode &= ~current_umask();
  2914. do {
  2915. err = nfs4_handle_exception(NFS_SERVER(dir),
  2916. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2917. &exception);
  2918. } while (exception.retry);
  2919. return err;
  2920. }
  2921. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2922. struct nfs_fsstat *fsstat)
  2923. {
  2924. struct nfs4_statfs_arg args = {
  2925. .fh = fhandle,
  2926. .bitmask = server->attr_bitmask,
  2927. };
  2928. struct nfs4_statfs_res res = {
  2929. .fsstat = fsstat,
  2930. };
  2931. struct rpc_message msg = {
  2932. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2933. .rpc_argp = &args,
  2934. .rpc_resp = &res,
  2935. };
  2936. nfs_fattr_init(fsstat->fattr);
  2937. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2938. }
  2939. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2940. {
  2941. struct nfs4_exception exception = { };
  2942. int err;
  2943. do {
  2944. err = nfs4_handle_exception(server,
  2945. _nfs4_proc_statfs(server, fhandle, fsstat),
  2946. &exception);
  2947. } while (exception.retry);
  2948. return err;
  2949. }
  2950. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2951. struct nfs_fsinfo *fsinfo)
  2952. {
  2953. struct nfs4_fsinfo_arg args = {
  2954. .fh = fhandle,
  2955. .bitmask = server->attr_bitmask,
  2956. };
  2957. struct nfs4_fsinfo_res res = {
  2958. .fsinfo = fsinfo,
  2959. };
  2960. struct rpc_message msg = {
  2961. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2962. .rpc_argp = &args,
  2963. .rpc_resp = &res,
  2964. };
  2965. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2966. }
  2967. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2968. {
  2969. struct nfs4_exception exception = { };
  2970. int err;
  2971. do {
  2972. err = nfs4_handle_exception(server,
  2973. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2974. &exception);
  2975. } while (exception.retry);
  2976. return err;
  2977. }
  2978. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2979. {
  2980. int error;
  2981. nfs_fattr_init(fsinfo->fattr);
  2982. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  2983. if (error == 0) {
  2984. /* block layout checks this! */
  2985. server->pnfs_blksize = fsinfo->blksize;
  2986. set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
  2987. }
  2988. return error;
  2989. }
  2990. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2991. struct nfs_pathconf *pathconf)
  2992. {
  2993. struct nfs4_pathconf_arg args = {
  2994. .fh = fhandle,
  2995. .bitmask = server->attr_bitmask,
  2996. };
  2997. struct nfs4_pathconf_res res = {
  2998. .pathconf = pathconf,
  2999. };
  3000. struct rpc_message msg = {
  3001. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  3002. .rpc_argp = &args,
  3003. .rpc_resp = &res,
  3004. };
  3005. /* None of the pathconf attributes are mandatory to implement */
  3006. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  3007. memset(pathconf, 0, sizeof(*pathconf));
  3008. return 0;
  3009. }
  3010. nfs_fattr_init(pathconf->fattr);
  3011. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3012. }
  3013. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  3014. struct nfs_pathconf *pathconf)
  3015. {
  3016. struct nfs4_exception exception = { };
  3017. int err;
  3018. do {
  3019. err = nfs4_handle_exception(server,
  3020. _nfs4_proc_pathconf(server, fhandle, pathconf),
  3021. &exception);
  3022. } while (exception.retry);
  3023. return err;
  3024. }
  3025. void __nfs4_read_done_cb(struct nfs_read_data *data)
  3026. {
  3027. nfs_invalidate_atime(data->header->inode);
  3028. }
  3029. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
  3030. {
  3031. struct nfs_server *server = NFS_SERVER(data->header->inode);
  3032. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  3033. rpc_restart_call_prepare(task);
  3034. return -EAGAIN;
  3035. }
  3036. __nfs4_read_done_cb(data);
  3037. if (task->tk_status > 0)
  3038. renew_lease(server, data->timestamp);
  3039. return 0;
  3040. }
  3041. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  3042. {
  3043. dprintk("--> %s\n", __func__);
  3044. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3045. return -EAGAIN;
  3046. return data->read_done_cb ? data->read_done_cb(task, data) :
  3047. nfs4_read_done_cb(task, data);
  3048. }
  3049. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  3050. {
  3051. data->timestamp = jiffies;
  3052. data->read_done_cb = nfs4_read_done_cb;
  3053. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  3054. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  3055. }
  3056. static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
  3057. {
  3058. nfs4_setup_sequence(NFS_SERVER(data->header->inode),
  3059. &data->args.seq_args,
  3060. &data->res.seq_res,
  3061. task);
  3062. }
  3063. static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
  3064. {
  3065. struct inode *inode = data->header->inode;
  3066. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  3067. rpc_restart_call_prepare(task);
  3068. return -EAGAIN;
  3069. }
  3070. if (task->tk_status >= 0) {
  3071. renew_lease(NFS_SERVER(inode), data->timestamp);
  3072. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  3073. }
  3074. return 0;
  3075. }
  3076. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  3077. {
  3078. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3079. return -EAGAIN;
  3080. return data->write_done_cb ? data->write_done_cb(task, data) :
  3081. nfs4_write_done_cb(task, data);
  3082. }
  3083. static
  3084. bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
  3085. {
  3086. const struct nfs_pgio_header *hdr = data->header;
  3087. /* Don't request attributes for pNFS or O_DIRECT writes */
  3088. if (data->ds_clp != NULL || hdr->dreq != NULL)
  3089. return false;
  3090. /* Otherwise, request attributes if and only if we don't hold
  3091. * a delegation
  3092. */
  3093. return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
  3094. }
  3095. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  3096. {
  3097. struct nfs_server *server = NFS_SERVER(data->header->inode);
  3098. if (!nfs4_write_need_cache_consistency_data(data)) {
  3099. data->args.bitmask = NULL;
  3100. data->res.fattr = NULL;
  3101. } else
  3102. data->args.bitmask = server->cache_consistency_bitmask;
  3103. if (!data->write_done_cb)
  3104. data->write_done_cb = nfs4_write_done_cb;
  3105. data->res.server = server;
  3106. data->timestamp = jiffies;
  3107. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  3108. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3109. }
  3110. static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
  3111. {
  3112. nfs4_setup_sequence(NFS_SERVER(data->header->inode),
  3113. &data->args.seq_args,
  3114. &data->res.seq_res,
  3115. task);
  3116. }
  3117. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  3118. {
  3119. nfs4_setup_sequence(NFS_SERVER(data->inode),
  3120. &data->args.seq_args,
  3121. &data->res.seq_res,
  3122. task);
  3123. }
  3124. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  3125. {
  3126. struct inode *inode = data->inode;
  3127. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  3128. rpc_restart_call_prepare(task);
  3129. return -EAGAIN;
  3130. }
  3131. return 0;
  3132. }
  3133. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  3134. {
  3135. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3136. return -EAGAIN;
  3137. return data->commit_done_cb(task, data);
  3138. }
  3139. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
  3140. {
  3141. struct nfs_server *server = NFS_SERVER(data->inode);
  3142. if (data->commit_done_cb == NULL)
  3143. data->commit_done_cb = nfs4_commit_done_cb;
  3144. data->res.server = server;
  3145. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  3146. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3147. }
  3148. struct nfs4_renewdata {
  3149. struct nfs_client *client;
  3150. unsigned long timestamp;
  3151. };
  3152. /*
  3153. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  3154. * standalone procedure for queueing an asynchronous RENEW.
  3155. */
  3156. static void nfs4_renew_release(void *calldata)
  3157. {
  3158. struct nfs4_renewdata *data = calldata;
  3159. struct nfs_client *clp = data->client;
  3160. if (atomic_read(&clp->cl_count) > 1)
  3161. nfs4_schedule_state_renewal(clp);
  3162. nfs_put_client(clp);
  3163. kfree(data);
  3164. }
  3165. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  3166. {
  3167. struct nfs4_renewdata *data = calldata;
  3168. struct nfs_client *clp = data->client;
  3169. unsigned long timestamp = data->timestamp;
  3170. if (task->tk_status < 0) {
  3171. /* Unless we're shutting down, schedule state recovery! */
  3172. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  3173. return;
  3174. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  3175. nfs4_schedule_lease_recovery(clp);
  3176. return;
  3177. }
  3178. nfs4_schedule_path_down_recovery(clp);
  3179. }
  3180. do_renew_lease(clp, timestamp);
  3181. }
  3182. static const struct rpc_call_ops nfs4_renew_ops = {
  3183. .rpc_call_done = nfs4_renew_done,
  3184. .rpc_release = nfs4_renew_release,
  3185. };
  3186. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  3187. {
  3188. struct rpc_message msg = {
  3189. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3190. .rpc_argp = clp,
  3191. .rpc_cred = cred,
  3192. };
  3193. struct nfs4_renewdata *data;
  3194. if (renew_flags == 0)
  3195. return 0;
  3196. if (!atomic_inc_not_zero(&clp->cl_count))
  3197. return -EIO;
  3198. data = kmalloc(sizeof(*data), GFP_NOFS);
  3199. if (data == NULL)
  3200. return -ENOMEM;
  3201. data->client = clp;
  3202. data->timestamp = jiffies;
  3203. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  3204. &nfs4_renew_ops, data);
  3205. }
  3206. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  3207. {
  3208. struct rpc_message msg = {
  3209. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3210. .rpc_argp = clp,
  3211. .rpc_cred = cred,
  3212. };
  3213. unsigned long now = jiffies;
  3214. int status;
  3215. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3216. if (status < 0)
  3217. return status;
  3218. do_renew_lease(clp, now);
  3219. return 0;
  3220. }
  3221. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  3222. {
  3223. return (server->caps & NFS_CAP_ACLS)
  3224. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3225. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  3226. }
  3227. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  3228. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  3229. * the stack.
  3230. */
  3231. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  3232. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  3233. struct page **pages, unsigned int *pgbase)
  3234. {
  3235. struct page *newpage, **spages;
  3236. int rc = 0;
  3237. size_t len;
  3238. spages = pages;
  3239. do {
  3240. len = min_t(size_t, PAGE_SIZE, buflen);
  3241. newpage = alloc_page(GFP_KERNEL);
  3242. if (newpage == NULL)
  3243. goto unwind;
  3244. memcpy(page_address(newpage), buf, len);
  3245. buf += len;
  3246. buflen -= len;
  3247. *pages++ = newpage;
  3248. rc++;
  3249. } while (buflen != 0);
  3250. return rc;
  3251. unwind:
  3252. for(; rc > 0; rc--)
  3253. __free_page(spages[rc-1]);
  3254. return -ENOMEM;
  3255. }
  3256. struct nfs4_cached_acl {
  3257. int cached;
  3258. size_t len;
  3259. char data[0];
  3260. };
  3261. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  3262. {
  3263. struct nfs_inode *nfsi = NFS_I(inode);
  3264. spin_lock(&inode->i_lock);
  3265. kfree(nfsi->nfs4_acl);
  3266. nfsi->nfs4_acl = acl;
  3267. spin_unlock(&inode->i_lock);
  3268. }
  3269. static void nfs4_zap_acl_attr(struct inode *inode)
  3270. {
  3271. nfs4_set_cached_acl(inode, NULL);
  3272. }
  3273. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  3274. {
  3275. struct nfs_inode *nfsi = NFS_I(inode);
  3276. struct nfs4_cached_acl *acl;
  3277. int ret = -ENOENT;
  3278. spin_lock(&inode->i_lock);
  3279. acl = nfsi->nfs4_acl;
  3280. if (acl == NULL)
  3281. goto out;
  3282. if (buf == NULL) /* user is just asking for length */
  3283. goto out_len;
  3284. if (acl->cached == 0)
  3285. goto out;
  3286. ret = -ERANGE; /* see getxattr(2) man page */
  3287. if (acl->len > buflen)
  3288. goto out;
  3289. memcpy(buf, acl->data, acl->len);
  3290. out_len:
  3291. ret = acl->len;
  3292. out:
  3293. spin_unlock(&inode->i_lock);
  3294. return ret;
  3295. }
  3296. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  3297. {
  3298. struct nfs4_cached_acl *acl;
  3299. size_t buflen = sizeof(*acl) + acl_len;
  3300. if (buflen <= PAGE_SIZE) {
  3301. acl = kmalloc(buflen, GFP_KERNEL);
  3302. if (acl == NULL)
  3303. goto out;
  3304. acl->cached = 1;
  3305. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  3306. } else {
  3307. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  3308. if (acl == NULL)
  3309. goto out;
  3310. acl->cached = 0;
  3311. }
  3312. acl->len = acl_len;
  3313. out:
  3314. nfs4_set_cached_acl(inode, acl);
  3315. }
  3316. /*
  3317. * The getxattr API returns the required buffer length when called with a
  3318. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  3319. * the required buf. On a NULL buf, we send a page of data to the server
  3320. * guessing that the ACL request can be serviced by a page. If so, we cache
  3321. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  3322. * the cache. If not so, we throw away the page, and cache the required
  3323. * length. The next getxattr call will then produce another round trip to
  3324. * the server, this time with the input buf of the required size.
  3325. */
  3326. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3327. {
  3328. struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
  3329. struct nfs_getaclargs args = {
  3330. .fh = NFS_FH(inode),
  3331. .acl_pages = pages,
  3332. .acl_len = buflen,
  3333. };
  3334. struct nfs_getaclres res = {
  3335. .acl_len = buflen,
  3336. };
  3337. struct rpc_message msg = {
  3338. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  3339. .rpc_argp = &args,
  3340. .rpc_resp = &res,
  3341. };
  3342. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  3343. int ret = -ENOMEM, i;
  3344. /* As long as we're doing a round trip to the server anyway,
  3345. * let's be prepared for a page of acl data. */
  3346. if (npages == 0)
  3347. npages = 1;
  3348. if (npages > ARRAY_SIZE(pages))
  3349. return -ERANGE;
  3350. for (i = 0; i < npages; i++) {
  3351. pages[i] = alloc_page(GFP_KERNEL);
  3352. if (!pages[i])
  3353. goto out_free;
  3354. }
  3355. /* for decoding across pages */
  3356. res.acl_scratch = alloc_page(GFP_KERNEL);
  3357. if (!res.acl_scratch)
  3358. goto out_free;
  3359. args.acl_len = npages * PAGE_SIZE;
  3360. args.acl_pgbase = 0;
  3361. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  3362. __func__, buf, buflen, npages, args.acl_len);
  3363. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  3364. &msg, &args.seq_args, &res.seq_res, 0);
  3365. if (ret)
  3366. goto out_free;
  3367. /* Handle the case where the passed-in buffer is too short */
  3368. if (res.acl_flags & NFS4_ACL_TRUNC) {
  3369. /* Did the user only issue a request for the acl length? */
  3370. if (buf == NULL)
  3371. goto out_ok;
  3372. ret = -ERANGE;
  3373. goto out_free;
  3374. }
  3375. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
  3376. if (buf)
  3377. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  3378. out_ok:
  3379. ret = res.acl_len;
  3380. out_free:
  3381. for (i = 0; i < npages; i++)
  3382. if (pages[i])
  3383. __free_page(pages[i]);
  3384. if (res.acl_scratch)
  3385. __free_page(res.acl_scratch);
  3386. return ret;
  3387. }
  3388. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3389. {
  3390. struct nfs4_exception exception = { };
  3391. ssize_t ret;
  3392. do {
  3393. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3394. if (ret >= 0)
  3395. break;
  3396. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3397. } while (exception.retry);
  3398. return ret;
  3399. }
  3400. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3401. {
  3402. struct nfs_server *server = NFS_SERVER(inode);
  3403. int ret;
  3404. if (!nfs4_server_supports_acls(server))
  3405. return -EOPNOTSUPP;
  3406. ret = nfs_revalidate_inode(server, inode);
  3407. if (ret < 0)
  3408. return ret;
  3409. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  3410. nfs_zap_acl_cache(inode);
  3411. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3412. if (ret != -ENOENT)
  3413. /* -ENOENT is returned if there is no ACL or if there is an ACL
  3414. * but no cached acl data, just the acl length */
  3415. return ret;
  3416. return nfs4_get_acl_uncached(inode, buf, buflen);
  3417. }
  3418. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3419. {
  3420. struct nfs_server *server = NFS_SERVER(inode);
  3421. struct page *pages[NFS4ACL_MAXPAGES];
  3422. struct nfs_setaclargs arg = {
  3423. .fh = NFS_FH(inode),
  3424. .acl_pages = pages,
  3425. .acl_len = buflen,
  3426. };
  3427. struct nfs_setaclres res;
  3428. struct rpc_message msg = {
  3429. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3430. .rpc_argp = &arg,
  3431. .rpc_resp = &res,
  3432. };
  3433. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  3434. int ret, i;
  3435. if (!nfs4_server_supports_acls(server))
  3436. return -EOPNOTSUPP;
  3437. if (npages > ARRAY_SIZE(pages))
  3438. return -ERANGE;
  3439. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3440. if (i < 0)
  3441. return i;
  3442. nfs4_inode_return_delegation(inode);
  3443. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3444. /*
  3445. * Free each page after tx, so the only ref left is
  3446. * held by the network stack
  3447. */
  3448. for (; i > 0; i--)
  3449. put_page(pages[i-1]);
  3450. /*
  3451. * Acl update can result in inode attribute update.
  3452. * so mark the attribute cache invalid.
  3453. */
  3454. spin_lock(&inode->i_lock);
  3455. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  3456. spin_unlock(&inode->i_lock);
  3457. nfs_access_zap_cache(inode);
  3458. nfs_zap_acl_cache(inode);
  3459. return ret;
  3460. }
  3461. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3462. {
  3463. struct nfs4_exception exception = { };
  3464. int err;
  3465. do {
  3466. err = nfs4_handle_exception(NFS_SERVER(inode),
  3467. __nfs4_proc_set_acl(inode, buf, buflen),
  3468. &exception);
  3469. } while (exception.retry);
  3470. return err;
  3471. }
  3472. static int
  3473. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3474. {
  3475. struct nfs_client *clp = server->nfs_client;
  3476. if (task->tk_status >= 0)
  3477. return 0;
  3478. switch(task->tk_status) {
  3479. case -NFS4ERR_DELEG_REVOKED:
  3480. case -NFS4ERR_ADMIN_REVOKED:
  3481. case -NFS4ERR_BAD_STATEID:
  3482. if (state == NULL)
  3483. break;
  3484. nfs_remove_bad_delegation(state->inode);
  3485. case -NFS4ERR_OPENMODE:
  3486. if (state == NULL)
  3487. break;
  3488. nfs4_schedule_stateid_recovery(server, state);
  3489. goto wait_on_recovery;
  3490. case -NFS4ERR_EXPIRED:
  3491. if (state != NULL)
  3492. nfs4_schedule_stateid_recovery(server, state);
  3493. case -NFS4ERR_STALE_STATEID:
  3494. case -NFS4ERR_STALE_CLIENTID:
  3495. nfs4_schedule_lease_recovery(clp);
  3496. goto wait_on_recovery;
  3497. #if defined(CONFIG_NFS_V4_1)
  3498. case -NFS4ERR_BADSESSION:
  3499. case -NFS4ERR_BADSLOT:
  3500. case -NFS4ERR_BAD_HIGH_SLOT:
  3501. case -NFS4ERR_DEADSESSION:
  3502. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3503. case -NFS4ERR_SEQ_FALSE_RETRY:
  3504. case -NFS4ERR_SEQ_MISORDERED:
  3505. dprintk("%s ERROR %d, Reset session\n", __func__,
  3506. task->tk_status);
  3507. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  3508. task->tk_status = 0;
  3509. return -EAGAIN;
  3510. #endif /* CONFIG_NFS_V4_1 */
  3511. case -NFS4ERR_DELAY:
  3512. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3513. case -NFS4ERR_GRACE:
  3514. case -EKEYEXPIRED:
  3515. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3516. task->tk_status = 0;
  3517. return -EAGAIN;
  3518. case -NFS4ERR_RETRY_UNCACHED_REP:
  3519. case -NFS4ERR_OLD_STATEID:
  3520. task->tk_status = 0;
  3521. return -EAGAIN;
  3522. }
  3523. task->tk_status = nfs4_map_errors(task->tk_status);
  3524. return 0;
  3525. wait_on_recovery:
  3526. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3527. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3528. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3529. task->tk_status = 0;
  3530. return -EAGAIN;
  3531. }
  3532. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  3533. nfs4_verifier *bootverf)
  3534. {
  3535. __be32 verf[2];
  3536. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  3537. /* An impossible timestamp guarantees this value
  3538. * will never match a generated boot time. */
  3539. verf[0] = 0;
  3540. verf[1] = (__be32)(NSEC_PER_SEC + 1);
  3541. } else {
  3542. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  3543. verf[0] = (__be32)nn->boot_time.tv_sec;
  3544. verf[1] = (__be32)nn->boot_time.tv_nsec;
  3545. }
  3546. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  3547. }
  3548. static unsigned int
  3549. nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
  3550. char *buf, size_t len)
  3551. {
  3552. unsigned int result;
  3553. rcu_read_lock();
  3554. result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
  3555. clp->cl_ipaddr,
  3556. rpc_peeraddr2str(clp->cl_rpcclient,
  3557. RPC_DISPLAY_ADDR),
  3558. rpc_peeraddr2str(clp->cl_rpcclient,
  3559. RPC_DISPLAY_PROTO));
  3560. rcu_read_unlock();
  3561. return result;
  3562. }
  3563. static unsigned int
  3564. nfs4_init_uniform_client_string(const struct nfs_client *clp,
  3565. char *buf, size_t len)
  3566. {
  3567. char *nodename = clp->cl_rpcclient->cl_nodename;
  3568. if (nfs4_client_id_uniquifier[0] != '\0')
  3569. nodename = nfs4_client_id_uniquifier;
  3570. return scnprintf(buf, len, "Linux NFSv%u.%u %s",
  3571. clp->rpc_ops->version, clp->cl_minorversion,
  3572. nodename);
  3573. }
  3574. /**
  3575. * nfs4_proc_setclientid - Negotiate client ID
  3576. * @clp: state data structure
  3577. * @program: RPC program for NFSv4 callback service
  3578. * @port: IP port number for NFS4 callback service
  3579. * @cred: RPC credential to use for this call
  3580. * @res: where to place the result
  3581. *
  3582. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  3583. */
  3584. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  3585. unsigned short port, struct rpc_cred *cred,
  3586. struct nfs4_setclientid_res *res)
  3587. {
  3588. nfs4_verifier sc_verifier;
  3589. struct nfs4_setclientid setclientid = {
  3590. .sc_verifier = &sc_verifier,
  3591. .sc_prog = program,
  3592. .sc_cb_ident = clp->cl_cb_ident,
  3593. };
  3594. struct rpc_message msg = {
  3595. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3596. .rpc_argp = &setclientid,
  3597. .rpc_resp = res,
  3598. .rpc_cred = cred,
  3599. };
  3600. int status;
  3601. /* nfs_client_id4 */
  3602. nfs4_init_boot_verifier(clp, &sc_verifier);
  3603. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  3604. setclientid.sc_name_len =
  3605. nfs4_init_uniform_client_string(clp,
  3606. setclientid.sc_name,
  3607. sizeof(setclientid.sc_name));
  3608. else
  3609. setclientid.sc_name_len =
  3610. nfs4_init_nonuniform_client_string(clp,
  3611. setclientid.sc_name,
  3612. sizeof(setclientid.sc_name));
  3613. /* cb_client4 */
  3614. rcu_read_lock();
  3615. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3616. sizeof(setclientid.sc_netid),
  3617. rpc_peeraddr2str(clp->cl_rpcclient,
  3618. RPC_DISPLAY_NETID));
  3619. rcu_read_unlock();
  3620. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3621. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3622. clp->cl_ipaddr, port >> 8, port & 255);
  3623. dprintk("NFS call setclientid auth=%s, '%.*s'\n",
  3624. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3625. setclientid.sc_name_len, setclientid.sc_name);
  3626. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3627. dprintk("NFS reply setclientid: %d\n", status);
  3628. return status;
  3629. }
  3630. /**
  3631. * nfs4_proc_setclientid_confirm - Confirm client ID
  3632. * @clp: state data structure
  3633. * @res: result of a previous SETCLIENTID
  3634. * @cred: RPC credential to use for this call
  3635. *
  3636. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  3637. */
  3638. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  3639. struct nfs4_setclientid_res *arg,
  3640. struct rpc_cred *cred)
  3641. {
  3642. struct nfs_fsinfo fsinfo;
  3643. struct rpc_message msg = {
  3644. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3645. .rpc_argp = arg,
  3646. .rpc_resp = &fsinfo,
  3647. .rpc_cred = cred,
  3648. };
  3649. unsigned long now;
  3650. int status;
  3651. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  3652. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3653. clp->cl_clientid);
  3654. now = jiffies;
  3655. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3656. if (status == 0) {
  3657. spin_lock(&clp->cl_lock);
  3658. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3659. clp->cl_last_renewal = now;
  3660. spin_unlock(&clp->cl_lock);
  3661. }
  3662. dprintk("NFS reply setclientid_confirm: %d\n", status);
  3663. return status;
  3664. }
  3665. struct nfs4_delegreturndata {
  3666. struct nfs4_delegreturnargs args;
  3667. struct nfs4_delegreturnres res;
  3668. struct nfs_fh fh;
  3669. nfs4_stateid stateid;
  3670. unsigned long timestamp;
  3671. struct nfs_fattr fattr;
  3672. int rpc_status;
  3673. };
  3674. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3675. {
  3676. struct nfs4_delegreturndata *data = calldata;
  3677. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3678. return;
  3679. switch (task->tk_status) {
  3680. case -NFS4ERR_STALE_STATEID:
  3681. case -NFS4ERR_EXPIRED:
  3682. case 0:
  3683. renew_lease(data->res.server, data->timestamp);
  3684. break;
  3685. default:
  3686. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3687. -EAGAIN) {
  3688. rpc_restart_call_prepare(task);
  3689. return;
  3690. }
  3691. }
  3692. data->rpc_status = task->tk_status;
  3693. }
  3694. static void nfs4_delegreturn_release(void *calldata)
  3695. {
  3696. kfree(calldata);
  3697. }
  3698. #if defined(CONFIG_NFS_V4_1)
  3699. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3700. {
  3701. struct nfs4_delegreturndata *d_data;
  3702. d_data = (struct nfs4_delegreturndata *)data;
  3703. nfs4_setup_sequence(d_data->res.server,
  3704. &d_data->args.seq_args,
  3705. &d_data->res.seq_res,
  3706. task);
  3707. }
  3708. #endif /* CONFIG_NFS_V4_1 */
  3709. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3710. #if defined(CONFIG_NFS_V4_1)
  3711. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3712. #endif /* CONFIG_NFS_V4_1 */
  3713. .rpc_call_done = nfs4_delegreturn_done,
  3714. .rpc_release = nfs4_delegreturn_release,
  3715. };
  3716. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3717. {
  3718. struct nfs4_delegreturndata *data;
  3719. struct nfs_server *server = NFS_SERVER(inode);
  3720. struct rpc_task *task;
  3721. struct rpc_message msg = {
  3722. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3723. .rpc_cred = cred,
  3724. };
  3725. struct rpc_task_setup task_setup_data = {
  3726. .rpc_client = server->client,
  3727. .rpc_message = &msg,
  3728. .callback_ops = &nfs4_delegreturn_ops,
  3729. .flags = RPC_TASK_ASYNC,
  3730. };
  3731. int status = 0;
  3732. data = kzalloc(sizeof(*data), GFP_NOFS);
  3733. if (data == NULL)
  3734. return -ENOMEM;
  3735. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3736. data->args.fhandle = &data->fh;
  3737. data->args.stateid = &data->stateid;
  3738. data->args.bitmask = server->cache_consistency_bitmask;
  3739. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3740. nfs4_stateid_copy(&data->stateid, stateid);
  3741. data->res.fattr = &data->fattr;
  3742. data->res.server = server;
  3743. nfs_fattr_init(data->res.fattr);
  3744. data->timestamp = jiffies;
  3745. data->rpc_status = 0;
  3746. task_setup_data.callback_data = data;
  3747. msg.rpc_argp = &data->args;
  3748. msg.rpc_resp = &data->res;
  3749. task = rpc_run_task(&task_setup_data);
  3750. if (IS_ERR(task))
  3751. return PTR_ERR(task);
  3752. if (!issync)
  3753. goto out;
  3754. status = nfs4_wait_for_completion_rpc_task(task);
  3755. if (status != 0)
  3756. goto out;
  3757. status = data->rpc_status;
  3758. if (status == 0)
  3759. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  3760. else
  3761. nfs_refresh_inode(inode, &data->fattr);
  3762. out:
  3763. rpc_put_task(task);
  3764. return status;
  3765. }
  3766. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3767. {
  3768. struct nfs_server *server = NFS_SERVER(inode);
  3769. struct nfs4_exception exception = { };
  3770. int err;
  3771. do {
  3772. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3773. switch (err) {
  3774. case -NFS4ERR_STALE_STATEID:
  3775. case -NFS4ERR_EXPIRED:
  3776. case 0:
  3777. return 0;
  3778. }
  3779. err = nfs4_handle_exception(server, err, &exception);
  3780. } while (exception.retry);
  3781. return err;
  3782. }
  3783. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3784. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3785. /*
  3786. * sleep, with exponential backoff, and retry the LOCK operation.
  3787. */
  3788. static unsigned long
  3789. nfs4_set_lock_task_retry(unsigned long timeout)
  3790. {
  3791. freezable_schedule_timeout_killable(timeout);
  3792. timeout <<= 1;
  3793. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3794. return NFS4_LOCK_MAXTIMEOUT;
  3795. return timeout;
  3796. }
  3797. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3798. {
  3799. struct inode *inode = state->inode;
  3800. struct nfs_server *server = NFS_SERVER(inode);
  3801. struct nfs_client *clp = server->nfs_client;
  3802. struct nfs_lockt_args arg = {
  3803. .fh = NFS_FH(inode),
  3804. .fl = request,
  3805. };
  3806. struct nfs_lockt_res res = {
  3807. .denied = request,
  3808. };
  3809. struct rpc_message msg = {
  3810. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3811. .rpc_argp = &arg,
  3812. .rpc_resp = &res,
  3813. .rpc_cred = state->owner->so_cred,
  3814. };
  3815. struct nfs4_lock_state *lsp;
  3816. int status;
  3817. arg.lock_owner.clientid = clp->cl_clientid;
  3818. status = nfs4_set_lock_state(state, request);
  3819. if (status != 0)
  3820. goto out;
  3821. lsp = request->fl_u.nfs4_fl.owner;
  3822. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  3823. arg.lock_owner.s_dev = server->s_dev;
  3824. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3825. switch (status) {
  3826. case 0:
  3827. request->fl_type = F_UNLCK;
  3828. break;
  3829. case -NFS4ERR_DENIED:
  3830. status = 0;
  3831. }
  3832. request->fl_ops->fl_release_private(request);
  3833. out:
  3834. return status;
  3835. }
  3836. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3837. {
  3838. struct nfs4_exception exception = { };
  3839. int err;
  3840. do {
  3841. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3842. _nfs4_proc_getlk(state, cmd, request),
  3843. &exception);
  3844. } while (exception.retry);
  3845. return err;
  3846. }
  3847. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3848. {
  3849. int res = 0;
  3850. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3851. case FL_POSIX:
  3852. res = posix_lock_file_wait(file, fl);
  3853. break;
  3854. case FL_FLOCK:
  3855. res = flock_lock_file_wait(file, fl);
  3856. break;
  3857. default:
  3858. BUG();
  3859. }
  3860. return res;
  3861. }
  3862. struct nfs4_unlockdata {
  3863. struct nfs_locku_args arg;
  3864. struct nfs_locku_res res;
  3865. struct nfs4_lock_state *lsp;
  3866. struct nfs_open_context *ctx;
  3867. struct file_lock fl;
  3868. const struct nfs_server *server;
  3869. unsigned long timestamp;
  3870. };
  3871. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3872. struct nfs_open_context *ctx,
  3873. struct nfs4_lock_state *lsp,
  3874. struct nfs_seqid *seqid)
  3875. {
  3876. struct nfs4_unlockdata *p;
  3877. struct inode *inode = lsp->ls_state->inode;
  3878. p = kzalloc(sizeof(*p), GFP_NOFS);
  3879. if (p == NULL)
  3880. return NULL;
  3881. p->arg.fh = NFS_FH(inode);
  3882. p->arg.fl = &p->fl;
  3883. p->arg.seqid = seqid;
  3884. p->res.seqid = seqid;
  3885. p->arg.stateid = &lsp->ls_stateid;
  3886. p->lsp = lsp;
  3887. atomic_inc(&lsp->ls_count);
  3888. /* Ensure we don't close file until we're done freeing locks! */
  3889. p->ctx = get_nfs_open_context(ctx);
  3890. memcpy(&p->fl, fl, sizeof(p->fl));
  3891. p->server = NFS_SERVER(inode);
  3892. return p;
  3893. }
  3894. static void nfs4_locku_release_calldata(void *data)
  3895. {
  3896. struct nfs4_unlockdata *calldata = data;
  3897. nfs_free_seqid(calldata->arg.seqid);
  3898. nfs4_put_lock_state(calldata->lsp);
  3899. put_nfs_open_context(calldata->ctx);
  3900. kfree(calldata);
  3901. }
  3902. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3903. {
  3904. struct nfs4_unlockdata *calldata = data;
  3905. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  3906. return;
  3907. switch (task->tk_status) {
  3908. case 0:
  3909. nfs4_stateid_copy(&calldata->lsp->ls_stateid,
  3910. &calldata->res.stateid);
  3911. renew_lease(calldata->server, calldata->timestamp);
  3912. break;
  3913. case -NFS4ERR_BAD_STATEID:
  3914. case -NFS4ERR_OLD_STATEID:
  3915. case -NFS4ERR_STALE_STATEID:
  3916. case -NFS4ERR_EXPIRED:
  3917. break;
  3918. default:
  3919. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3920. rpc_restart_call_prepare(task);
  3921. }
  3922. nfs_release_seqid(calldata->arg.seqid);
  3923. }
  3924. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3925. {
  3926. struct nfs4_unlockdata *calldata = data;
  3927. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3928. return;
  3929. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  3930. /* Note: exit _without_ running nfs4_locku_done */
  3931. task->tk_action = NULL;
  3932. nfs4_sequence_done(task, &calldata->res.seq_res);
  3933. return;
  3934. }
  3935. calldata->timestamp = jiffies;
  3936. if (nfs4_setup_sequence(calldata->server,
  3937. &calldata->arg.seq_args,
  3938. &calldata->res.seq_res,
  3939. task) != 0)
  3940. nfs_release_seqid(calldata->arg.seqid);
  3941. }
  3942. static const struct rpc_call_ops nfs4_locku_ops = {
  3943. .rpc_call_prepare = nfs4_locku_prepare,
  3944. .rpc_call_done = nfs4_locku_done,
  3945. .rpc_release = nfs4_locku_release_calldata,
  3946. };
  3947. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3948. struct nfs_open_context *ctx,
  3949. struct nfs4_lock_state *lsp,
  3950. struct nfs_seqid *seqid)
  3951. {
  3952. struct nfs4_unlockdata *data;
  3953. struct rpc_message msg = {
  3954. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3955. .rpc_cred = ctx->cred,
  3956. };
  3957. struct rpc_task_setup task_setup_data = {
  3958. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3959. .rpc_message = &msg,
  3960. .callback_ops = &nfs4_locku_ops,
  3961. .workqueue = nfsiod_workqueue,
  3962. .flags = RPC_TASK_ASYNC,
  3963. };
  3964. /* Ensure this is an unlock - when canceling a lock, the
  3965. * canceled lock is passed in, and it won't be an unlock.
  3966. */
  3967. fl->fl_type = F_UNLCK;
  3968. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3969. if (data == NULL) {
  3970. nfs_free_seqid(seqid);
  3971. return ERR_PTR(-ENOMEM);
  3972. }
  3973. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  3974. msg.rpc_argp = &data->arg;
  3975. msg.rpc_resp = &data->res;
  3976. task_setup_data.callback_data = data;
  3977. return rpc_run_task(&task_setup_data);
  3978. }
  3979. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3980. {
  3981. struct nfs_inode *nfsi = NFS_I(state->inode);
  3982. struct nfs_seqid *seqid;
  3983. struct nfs4_lock_state *lsp;
  3984. struct rpc_task *task;
  3985. int status = 0;
  3986. unsigned char fl_flags = request->fl_flags;
  3987. status = nfs4_set_lock_state(state, request);
  3988. /* Unlock _before_ we do the RPC call */
  3989. request->fl_flags |= FL_EXISTS;
  3990. down_read(&nfsi->rwsem);
  3991. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3992. up_read(&nfsi->rwsem);
  3993. goto out;
  3994. }
  3995. up_read(&nfsi->rwsem);
  3996. if (status != 0)
  3997. goto out;
  3998. /* Is this a delegated lock? */
  3999. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  4000. goto out;
  4001. lsp = request->fl_u.nfs4_fl.owner;
  4002. seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  4003. status = -ENOMEM;
  4004. if (seqid == NULL)
  4005. goto out;
  4006. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  4007. status = PTR_ERR(task);
  4008. if (IS_ERR(task))
  4009. goto out;
  4010. status = nfs4_wait_for_completion_rpc_task(task);
  4011. rpc_put_task(task);
  4012. out:
  4013. request->fl_flags = fl_flags;
  4014. return status;
  4015. }
  4016. struct nfs4_lockdata {
  4017. struct nfs_lock_args arg;
  4018. struct nfs_lock_res res;
  4019. struct nfs4_lock_state *lsp;
  4020. struct nfs_open_context *ctx;
  4021. struct file_lock fl;
  4022. unsigned long timestamp;
  4023. int rpc_status;
  4024. int cancelled;
  4025. struct nfs_server *server;
  4026. };
  4027. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  4028. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  4029. gfp_t gfp_mask)
  4030. {
  4031. struct nfs4_lockdata *p;
  4032. struct inode *inode = lsp->ls_state->inode;
  4033. struct nfs_server *server = NFS_SERVER(inode);
  4034. p = kzalloc(sizeof(*p), gfp_mask);
  4035. if (p == NULL)
  4036. return NULL;
  4037. p->arg.fh = NFS_FH(inode);
  4038. p->arg.fl = &p->fl;
  4039. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  4040. if (p->arg.open_seqid == NULL)
  4041. goto out_free;
  4042. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
  4043. if (p->arg.lock_seqid == NULL)
  4044. goto out_free_seqid;
  4045. p->arg.lock_stateid = &lsp->ls_stateid;
  4046. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  4047. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  4048. p->arg.lock_owner.s_dev = server->s_dev;
  4049. p->res.lock_seqid = p->arg.lock_seqid;
  4050. p->lsp = lsp;
  4051. p->server = server;
  4052. atomic_inc(&lsp->ls_count);
  4053. p->ctx = get_nfs_open_context(ctx);
  4054. memcpy(&p->fl, fl, sizeof(p->fl));
  4055. return p;
  4056. out_free_seqid:
  4057. nfs_free_seqid(p->arg.open_seqid);
  4058. out_free:
  4059. kfree(p);
  4060. return NULL;
  4061. }
  4062. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  4063. {
  4064. struct nfs4_lockdata *data = calldata;
  4065. struct nfs4_state *state = data->lsp->ls_state;
  4066. dprintk("%s: begin!\n", __func__);
  4067. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  4068. return;
  4069. /* Do we need to do an open_to_lock_owner? */
  4070. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  4071. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  4072. goto out_release_lock_seqid;
  4073. }
  4074. data->arg.open_stateid = &state->stateid;
  4075. data->arg.new_lock_owner = 1;
  4076. data->res.open_seqid = data->arg.open_seqid;
  4077. } else
  4078. data->arg.new_lock_owner = 0;
  4079. data->timestamp = jiffies;
  4080. if (nfs4_setup_sequence(data->server,
  4081. &data->arg.seq_args,
  4082. &data->res.seq_res,
  4083. task) == 0)
  4084. return;
  4085. nfs_release_seqid(data->arg.open_seqid);
  4086. out_release_lock_seqid:
  4087. nfs_release_seqid(data->arg.lock_seqid);
  4088. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  4089. }
  4090. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  4091. {
  4092. struct nfs4_lockdata *data = calldata;
  4093. dprintk("%s: begin!\n", __func__);
  4094. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4095. return;
  4096. data->rpc_status = task->tk_status;
  4097. if (data->arg.new_lock_owner != 0) {
  4098. if (data->rpc_status == 0)
  4099. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  4100. else
  4101. goto out;
  4102. }
  4103. if (data->rpc_status == 0) {
  4104. nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
  4105. set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
  4106. renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
  4107. }
  4108. out:
  4109. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  4110. }
  4111. static void nfs4_lock_release(void *calldata)
  4112. {
  4113. struct nfs4_lockdata *data = calldata;
  4114. dprintk("%s: begin!\n", __func__);
  4115. nfs_free_seqid(data->arg.open_seqid);
  4116. if (data->cancelled != 0) {
  4117. struct rpc_task *task;
  4118. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  4119. data->arg.lock_seqid);
  4120. if (!IS_ERR(task))
  4121. rpc_put_task_async(task);
  4122. dprintk("%s: cancelling lock!\n", __func__);
  4123. } else
  4124. nfs_free_seqid(data->arg.lock_seqid);
  4125. nfs4_put_lock_state(data->lsp);
  4126. put_nfs_open_context(data->ctx);
  4127. kfree(data);
  4128. dprintk("%s: done!\n", __func__);
  4129. }
  4130. static const struct rpc_call_ops nfs4_lock_ops = {
  4131. .rpc_call_prepare = nfs4_lock_prepare,
  4132. .rpc_call_done = nfs4_lock_done,
  4133. .rpc_release = nfs4_lock_release,
  4134. };
  4135. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  4136. {
  4137. switch (error) {
  4138. case -NFS4ERR_ADMIN_REVOKED:
  4139. case -NFS4ERR_BAD_STATEID:
  4140. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4141. if (new_lock_owner != 0 ||
  4142. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  4143. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  4144. break;
  4145. case -NFS4ERR_STALE_STATEID:
  4146. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4147. case -NFS4ERR_EXPIRED:
  4148. nfs4_schedule_lease_recovery(server->nfs_client);
  4149. };
  4150. }
  4151. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  4152. {
  4153. struct nfs4_lockdata *data;
  4154. struct rpc_task *task;
  4155. struct rpc_message msg = {
  4156. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  4157. .rpc_cred = state->owner->so_cred,
  4158. };
  4159. struct rpc_task_setup task_setup_data = {
  4160. .rpc_client = NFS_CLIENT(state->inode),
  4161. .rpc_message = &msg,
  4162. .callback_ops = &nfs4_lock_ops,
  4163. .workqueue = nfsiod_workqueue,
  4164. .flags = RPC_TASK_ASYNC,
  4165. };
  4166. int ret;
  4167. dprintk("%s: begin!\n", __func__);
  4168. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  4169. fl->fl_u.nfs4_fl.owner,
  4170. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  4171. if (data == NULL)
  4172. return -ENOMEM;
  4173. if (IS_SETLKW(cmd))
  4174. data->arg.block = 1;
  4175. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  4176. msg.rpc_argp = &data->arg;
  4177. msg.rpc_resp = &data->res;
  4178. task_setup_data.callback_data = data;
  4179. if (recovery_type > NFS_LOCK_NEW) {
  4180. if (recovery_type == NFS_LOCK_RECLAIM)
  4181. data->arg.reclaim = NFS_LOCK_RECLAIM;
  4182. nfs4_set_sequence_privileged(&data->arg.seq_args);
  4183. }
  4184. task = rpc_run_task(&task_setup_data);
  4185. if (IS_ERR(task))
  4186. return PTR_ERR(task);
  4187. ret = nfs4_wait_for_completion_rpc_task(task);
  4188. if (ret == 0) {
  4189. ret = data->rpc_status;
  4190. if (ret)
  4191. nfs4_handle_setlk_error(data->server, data->lsp,
  4192. data->arg.new_lock_owner, ret);
  4193. } else
  4194. data->cancelled = 1;
  4195. rpc_put_task(task);
  4196. dprintk("%s: done, ret = %d!\n", __func__, ret);
  4197. return ret;
  4198. }
  4199. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  4200. {
  4201. struct nfs_server *server = NFS_SERVER(state->inode);
  4202. struct nfs4_exception exception = {
  4203. .inode = state->inode,
  4204. };
  4205. int err;
  4206. do {
  4207. /* Cache the lock if possible... */
  4208. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4209. return 0;
  4210. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  4211. if (err != -NFS4ERR_DELAY)
  4212. break;
  4213. nfs4_handle_exception(server, err, &exception);
  4214. } while (exception.retry);
  4215. return err;
  4216. }
  4217. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4218. {
  4219. struct nfs_server *server = NFS_SERVER(state->inode);
  4220. struct nfs4_exception exception = {
  4221. .inode = state->inode,
  4222. };
  4223. int err;
  4224. err = nfs4_set_lock_state(state, request);
  4225. if (err != 0)
  4226. return err;
  4227. do {
  4228. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4229. return 0;
  4230. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  4231. switch (err) {
  4232. default:
  4233. goto out;
  4234. case -NFS4ERR_GRACE:
  4235. case -NFS4ERR_DELAY:
  4236. nfs4_handle_exception(server, err, &exception);
  4237. err = 0;
  4238. }
  4239. } while (exception.retry);
  4240. out:
  4241. return err;
  4242. }
  4243. #if defined(CONFIG_NFS_V4_1)
  4244. /**
  4245. * nfs41_check_expired_locks - possibly free a lock stateid
  4246. *
  4247. * @state: NFSv4 state for an inode
  4248. *
  4249. * Returns NFS_OK if recovery for this stateid is now finished.
  4250. * Otherwise a negative NFS4ERR value is returned.
  4251. */
  4252. static int nfs41_check_expired_locks(struct nfs4_state *state)
  4253. {
  4254. int status, ret = -NFS4ERR_BAD_STATEID;
  4255. struct nfs4_lock_state *lsp;
  4256. struct nfs_server *server = NFS_SERVER(state->inode);
  4257. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  4258. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  4259. status = nfs41_test_stateid(server, &lsp->ls_stateid);
  4260. if (status != NFS_OK) {
  4261. /* Free the stateid unless the server
  4262. * informs us the stateid is unrecognized. */
  4263. if (status != -NFS4ERR_BAD_STATEID)
  4264. nfs41_free_stateid(server,
  4265. &lsp->ls_stateid);
  4266. clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  4267. ret = status;
  4268. }
  4269. }
  4270. };
  4271. return ret;
  4272. }
  4273. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4274. {
  4275. int status = NFS_OK;
  4276. if (test_bit(LK_STATE_IN_USE, &state->flags))
  4277. status = nfs41_check_expired_locks(state);
  4278. if (status != NFS_OK)
  4279. status = nfs4_lock_expired(state, request);
  4280. return status;
  4281. }
  4282. #endif
  4283. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4284. {
  4285. struct nfs_inode *nfsi = NFS_I(state->inode);
  4286. unsigned char fl_flags = request->fl_flags;
  4287. int status = -ENOLCK;
  4288. if ((fl_flags & FL_POSIX) &&
  4289. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  4290. goto out;
  4291. /* Is this a delegated open? */
  4292. status = nfs4_set_lock_state(state, request);
  4293. if (status != 0)
  4294. goto out;
  4295. request->fl_flags |= FL_ACCESS;
  4296. status = do_vfs_lock(request->fl_file, request);
  4297. if (status < 0)
  4298. goto out;
  4299. down_read(&nfsi->rwsem);
  4300. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  4301. /* Yes: cache locks! */
  4302. /* ...but avoid races with delegation recall... */
  4303. request->fl_flags = fl_flags & ~FL_SLEEP;
  4304. status = do_vfs_lock(request->fl_file, request);
  4305. goto out_unlock;
  4306. }
  4307. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  4308. if (status != 0)
  4309. goto out_unlock;
  4310. /* Note: we always want to sleep here! */
  4311. request->fl_flags = fl_flags | FL_SLEEP;
  4312. if (do_vfs_lock(request->fl_file, request) < 0)
  4313. printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
  4314. "manager!\n", __func__);
  4315. out_unlock:
  4316. up_read(&nfsi->rwsem);
  4317. out:
  4318. request->fl_flags = fl_flags;
  4319. return status;
  4320. }
  4321. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4322. {
  4323. struct nfs4_exception exception = {
  4324. .state = state,
  4325. .inode = state->inode,
  4326. };
  4327. int err;
  4328. do {
  4329. err = _nfs4_proc_setlk(state, cmd, request);
  4330. if (err == -NFS4ERR_DENIED)
  4331. err = -EAGAIN;
  4332. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  4333. err, &exception);
  4334. } while (exception.retry);
  4335. return err;
  4336. }
  4337. static int
  4338. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  4339. {
  4340. struct nfs_open_context *ctx;
  4341. struct nfs4_state *state;
  4342. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  4343. int status;
  4344. /* verify open state */
  4345. ctx = nfs_file_open_context(filp);
  4346. state = ctx->state;
  4347. if (request->fl_start < 0 || request->fl_end < 0)
  4348. return -EINVAL;
  4349. if (IS_GETLK(cmd)) {
  4350. if (state != NULL)
  4351. return nfs4_proc_getlk(state, F_GETLK, request);
  4352. return 0;
  4353. }
  4354. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  4355. return -EINVAL;
  4356. if (request->fl_type == F_UNLCK) {
  4357. if (state != NULL)
  4358. return nfs4_proc_unlck(state, cmd, request);
  4359. return 0;
  4360. }
  4361. if (state == NULL)
  4362. return -ENOLCK;
  4363. /*
  4364. * Don't rely on the VFS having checked the file open mode,
  4365. * since it won't do this for flock() locks.
  4366. */
  4367. switch (request->fl_type) {
  4368. case F_RDLCK:
  4369. if (!(filp->f_mode & FMODE_READ))
  4370. return -EBADF;
  4371. break;
  4372. case F_WRLCK:
  4373. if (!(filp->f_mode & FMODE_WRITE))
  4374. return -EBADF;
  4375. }
  4376. do {
  4377. status = nfs4_proc_setlk(state, cmd, request);
  4378. if ((status != -EAGAIN) || IS_SETLK(cmd))
  4379. break;
  4380. timeout = nfs4_set_lock_task_retry(timeout);
  4381. status = -ERESTARTSYS;
  4382. if (signalled())
  4383. break;
  4384. } while(status < 0);
  4385. return status;
  4386. }
  4387. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  4388. {
  4389. struct nfs_server *server = NFS_SERVER(state->inode);
  4390. struct nfs4_exception exception = { };
  4391. int err;
  4392. err = nfs4_set_lock_state(state, fl);
  4393. if (err != 0)
  4394. goto out;
  4395. do {
  4396. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  4397. switch (err) {
  4398. default:
  4399. printk(KERN_ERR "NFS: %s: unhandled error "
  4400. "%d.\n", __func__, err);
  4401. case 0:
  4402. case -ESTALE:
  4403. goto out;
  4404. case -NFS4ERR_EXPIRED:
  4405. nfs4_schedule_stateid_recovery(server, state);
  4406. case -NFS4ERR_STALE_CLIENTID:
  4407. case -NFS4ERR_STALE_STATEID:
  4408. nfs4_schedule_lease_recovery(server->nfs_client);
  4409. goto out;
  4410. case -NFS4ERR_BADSESSION:
  4411. case -NFS4ERR_BADSLOT:
  4412. case -NFS4ERR_BAD_HIGH_SLOT:
  4413. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  4414. case -NFS4ERR_DEADSESSION:
  4415. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  4416. goto out;
  4417. case -ERESTARTSYS:
  4418. /*
  4419. * The show must go on: exit, but mark the
  4420. * stateid as needing recovery.
  4421. */
  4422. case -NFS4ERR_DELEG_REVOKED:
  4423. case -NFS4ERR_ADMIN_REVOKED:
  4424. case -NFS4ERR_BAD_STATEID:
  4425. case -NFS4ERR_OPENMODE:
  4426. nfs4_schedule_stateid_recovery(server, state);
  4427. err = 0;
  4428. goto out;
  4429. case -EKEYEXPIRED:
  4430. /*
  4431. * User RPCSEC_GSS context has expired.
  4432. * We cannot recover this stateid now, so
  4433. * skip it and allow recovery thread to
  4434. * proceed.
  4435. */
  4436. err = 0;
  4437. goto out;
  4438. case -ENOMEM:
  4439. case -NFS4ERR_DENIED:
  4440. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  4441. err = 0;
  4442. goto out;
  4443. case -NFS4ERR_DELAY:
  4444. break;
  4445. }
  4446. err = nfs4_handle_exception(server, err, &exception);
  4447. } while (exception.retry);
  4448. out:
  4449. return err;
  4450. }
  4451. struct nfs_release_lockowner_data {
  4452. struct nfs4_lock_state *lsp;
  4453. struct nfs_server *server;
  4454. struct nfs_release_lockowner_args args;
  4455. };
  4456. static void nfs4_release_lockowner_release(void *calldata)
  4457. {
  4458. struct nfs_release_lockowner_data *data = calldata;
  4459. nfs4_free_lock_state(data->server, data->lsp);
  4460. kfree(calldata);
  4461. }
  4462. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  4463. .rpc_release = nfs4_release_lockowner_release,
  4464. };
  4465. int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
  4466. {
  4467. struct nfs_server *server = lsp->ls_state->owner->so_server;
  4468. struct nfs_release_lockowner_data *data;
  4469. struct rpc_message msg = {
  4470. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  4471. };
  4472. if (server->nfs_client->cl_mvops->minor_version != 0)
  4473. return -EINVAL;
  4474. data = kmalloc(sizeof(*data), GFP_NOFS);
  4475. if (!data)
  4476. return -ENOMEM;
  4477. data->lsp = lsp;
  4478. data->server = server;
  4479. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  4480. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  4481. data->args.lock_owner.s_dev = server->s_dev;
  4482. msg.rpc_argp = &data->args;
  4483. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  4484. return 0;
  4485. }
  4486. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  4487. static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
  4488. const void *buf, size_t buflen,
  4489. int flags, int type)
  4490. {
  4491. if (strcmp(key, "") != 0)
  4492. return -EINVAL;
  4493. return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
  4494. }
  4495. static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
  4496. void *buf, size_t buflen, int type)
  4497. {
  4498. if (strcmp(key, "") != 0)
  4499. return -EINVAL;
  4500. return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
  4501. }
  4502. static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
  4503. size_t list_len, const char *name,
  4504. size_t name_len, int type)
  4505. {
  4506. size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
  4507. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  4508. return 0;
  4509. if (list && len <= list_len)
  4510. memcpy(list, XATTR_NAME_NFSV4_ACL, len);
  4511. return len;
  4512. }
  4513. /*
  4514. * nfs_fhget will use either the mounted_on_fileid or the fileid
  4515. */
  4516. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  4517. {
  4518. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  4519. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  4520. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  4521. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  4522. return;
  4523. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4524. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  4525. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4526. fattr->nlink = 2;
  4527. }
  4528. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4529. const struct qstr *name,
  4530. struct nfs4_fs_locations *fs_locations,
  4531. struct page *page)
  4532. {
  4533. struct nfs_server *server = NFS_SERVER(dir);
  4534. u32 bitmask[2] = {
  4535. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4536. };
  4537. struct nfs4_fs_locations_arg args = {
  4538. .dir_fh = NFS_FH(dir),
  4539. .name = name,
  4540. .page = page,
  4541. .bitmask = bitmask,
  4542. };
  4543. struct nfs4_fs_locations_res res = {
  4544. .fs_locations = fs_locations,
  4545. };
  4546. struct rpc_message msg = {
  4547. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4548. .rpc_argp = &args,
  4549. .rpc_resp = &res,
  4550. };
  4551. int status;
  4552. dprintk("%s: start\n", __func__);
  4553. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  4554. * is not supported */
  4555. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  4556. bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
  4557. else
  4558. bitmask[0] |= FATTR4_WORD0_FILEID;
  4559. nfs_fattr_init(&fs_locations->fattr);
  4560. fs_locations->server = server;
  4561. fs_locations->nlocations = 0;
  4562. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4563. dprintk("%s: returned status = %d\n", __func__, status);
  4564. return status;
  4565. }
  4566. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4567. const struct qstr *name,
  4568. struct nfs4_fs_locations *fs_locations,
  4569. struct page *page)
  4570. {
  4571. struct nfs4_exception exception = { };
  4572. int err;
  4573. do {
  4574. err = nfs4_handle_exception(NFS_SERVER(dir),
  4575. _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
  4576. &exception);
  4577. } while (exception.retry);
  4578. return err;
  4579. }
  4580. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
  4581. {
  4582. int status;
  4583. struct nfs4_secinfo_arg args = {
  4584. .dir_fh = NFS_FH(dir),
  4585. .name = name,
  4586. };
  4587. struct nfs4_secinfo_res res = {
  4588. .flavors = flavors,
  4589. };
  4590. struct rpc_message msg = {
  4591. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  4592. .rpc_argp = &args,
  4593. .rpc_resp = &res,
  4594. };
  4595. dprintk("NFS call secinfo %s\n", name->name);
  4596. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  4597. dprintk("NFS reply secinfo: %d\n", status);
  4598. return status;
  4599. }
  4600. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  4601. struct nfs4_secinfo_flavors *flavors)
  4602. {
  4603. struct nfs4_exception exception = { };
  4604. int err;
  4605. do {
  4606. err = nfs4_handle_exception(NFS_SERVER(dir),
  4607. _nfs4_proc_secinfo(dir, name, flavors),
  4608. &exception);
  4609. } while (exception.retry);
  4610. return err;
  4611. }
  4612. #ifdef CONFIG_NFS_V4_1
  4613. /*
  4614. * Check the exchange flags returned by the server for invalid flags, having
  4615. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  4616. * DS flags set.
  4617. */
  4618. static int nfs4_check_cl_exchange_flags(u32 flags)
  4619. {
  4620. if (flags & ~EXCHGID4_FLAG_MASK_R)
  4621. goto out_inval;
  4622. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  4623. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  4624. goto out_inval;
  4625. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  4626. goto out_inval;
  4627. return NFS_OK;
  4628. out_inval:
  4629. return -NFS4ERR_INVAL;
  4630. }
  4631. static bool
  4632. nfs41_same_server_scope(struct nfs41_server_scope *a,
  4633. struct nfs41_server_scope *b)
  4634. {
  4635. if (a->server_scope_sz == b->server_scope_sz &&
  4636. memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
  4637. return true;
  4638. return false;
  4639. }
  4640. /*
  4641. * nfs4_proc_bind_conn_to_session()
  4642. *
  4643. * The 4.1 client currently uses the same TCP connection for the
  4644. * fore and backchannel.
  4645. */
  4646. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
  4647. {
  4648. int status;
  4649. struct nfs41_bind_conn_to_session_res res;
  4650. struct rpc_message msg = {
  4651. .rpc_proc =
  4652. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  4653. .rpc_argp = clp,
  4654. .rpc_resp = &res,
  4655. .rpc_cred = cred,
  4656. };
  4657. dprintk("--> %s\n", __func__);
  4658. res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  4659. if (unlikely(res.session == NULL)) {
  4660. status = -ENOMEM;
  4661. goto out;
  4662. }
  4663. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4664. if (status == 0) {
  4665. if (memcmp(res.session->sess_id.data,
  4666. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  4667. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  4668. status = -EIO;
  4669. goto out_session;
  4670. }
  4671. if (res.dir != NFS4_CDFS4_BOTH) {
  4672. dprintk("NFS: %s: Unexpected direction from server\n",
  4673. __func__);
  4674. status = -EIO;
  4675. goto out_session;
  4676. }
  4677. if (res.use_conn_in_rdma_mode) {
  4678. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  4679. __func__);
  4680. status = -EIO;
  4681. goto out_session;
  4682. }
  4683. }
  4684. out_session:
  4685. kfree(res.session);
  4686. out:
  4687. dprintk("<-- %s status= %d\n", __func__, status);
  4688. return status;
  4689. }
  4690. /*
  4691. * nfs4_proc_exchange_id()
  4692. *
  4693. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4694. *
  4695. * Since the clientid has expired, all compounds using sessions
  4696. * associated with the stale clientid will be returning
  4697. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4698. * be in some phase of session reset.
  4699. */
  4700. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4701. {
  4702. nfs4_verifier verifier;
  4703. struct nfs41_exchange_id_args args = {
  4704. .verifier = &verifier,
  4705. .client = clp,
  4706. .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
  4707. };
  4708. struct nfs41_exchange_id_res res = {
  4709. 0
  4710. };
  4711. int status;
  4712. struct rpc_message msg = {
  4713. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4714. .rpc_argp = &args,
  4715. .rpc_resp = &res,
  4716. .rpc_cred = cred,
  4717. };
  4718. nfs4_init_boot_verifier(clp, &verifier);
  4719. args.id_len = nfs4_init_uniform_client_string(clp, args.id,
  4720. sizeof(args.id));
  4721. dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
  4722. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4723. args.id_len, args.id);
  4724. res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
  4725. GFP_NOFS);
  4726. if (unlikely(res.server_owner == NULL)) {
  4727. status = -ENOMEM;
  4728. goto out;
  4729. }
  4730. res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
  4731. GFP_NOFS);
  4732. if (unlikely(res.server_scope == NULL)) {
  4733. status = -ENOMEM;
  4734. goto out_server_owner;
  4735. }
  4736. res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
  4737. if (unlikely(res.impl_id == NULL)) {
  4738. status = -ENOMEM;
  4739. goto out_server_scope;
  4740. }
  4741. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4742. if (status == 0)
  4743. status = nfs4_check_cl_exchange_flags(res.flags);
  4744. if (status == 0) {
  4745. clp->cl_clientid = res.clientid;
  4746. clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
  4747. if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
  4748. clp->cl_seqid = res.seqid;
  4749. kfree(clp->cl_serverowner);
  4750. clp->cl_serverowner = res.server_owner;
  4751. res.server_owner = NULL;
  4752. /* use the most recent implementation id */
  4753. kfree(clp->cl_implid);
  4754. clp->cl_implid = res.impl_id;
  4755. if (clp->cl_serverscope != NULL &&
  4756. !nfs41_same_server_scope(clp->cl_serverscope,
  4757. res.server_scope)) {
  4758. dprintk("%s: server_scope mismatch detected\n",
  4759. __func__);
  4760. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  4761. kfree(clp->cl_serverscope);
  4762. clp->cl_serverscope = NULL;
  4763. }
  4764. if (clp->cl_serverscope == NULL) {
  4765. clp->cl_serverscope = res.server_scope;
  4766. goto out;
  4767. }
  4768. } else
  4769. kfree(res.impl_id);
  4770. out_server_owner:
  4771. kfree(res.server_owner);
  4772. out_server_scope:
  4773. kfree(res.server_scope);
  4774. out:
  4775. if (clp->cl_implid != NULL)
  4776. dprintk("NFS reply exchange_id: Server Implementation ID: "
  4777. "domain: %s, name: %s, date: %llu,%u\n",
  4778. clp->cl_implid->domain, clp->cl_implid->name,
  4779. clp->cl_implid->date.seconds,
  4780. clp->cl_implid->date.nseconds);
  4781. dprintk("NFS reply exchange_id: %d\n", status);
  4782. return status;
  4783. }
  4784. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  4785. struct rpc_cred *cred)
  4786. {
  4787. struct rpc_message msg = {
  4788. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  4789. .rpc_argp = clp,
  4790. .rpc_cred = cred,
  4791. };
  4792. int status;
  4793. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4794. if (status)
  4795. dprintk("NFS: Got error %d from the server %s on "
  4796. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  4797. return status;
  4798. }
  4799. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  4800. struct rpc_cred *cred)
  4801. {
  4802. unsigned int loop;
  4803. int ret;
  4804. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  4805. ret = _nfs4_proc_destroy_clientid(clp, cred);
  4806. switch (ret) {
  4807. case -NFS4ERR_DELAY:
  4808. case -NFS4ERR_CLIENTID_BUSY:
  4809. ssleep(1);
  4810. break;
  4811. default:
  4812. return ret;
  4813. }
  4814. }
  4815. return 0;
  4816. }
  4817. int nfs4_destroy_clientid(struct nfs_client *clp)
  4818. {
  4819. struct rpc_cred *cred;
  4820. int ret = 0;
  4821. if (clp->cl_mvops->minor_version < 1)
  4822. goto out;
  4823. if (clp->cl_exchange_flags == 0)
  4824. goto out;
  4825. if (clp->cl_preserve_clid)
  4826. goto out;
  4827. cred = nfs4_get_exchange_id_cred(clp);
  4828. ret = nfs4_proc_destroy_clientid(clp, cred);
  4829. if (cred)
  4830. put_rpccred(cred);
  4831. switch (ret) {
  4832. case 0:
  4833. case -NFS4ERR_STALE_CLIENTID:
  4834. clp->cl_exchange_flags = 0;
  4835. }
  4836. out:
  4837. return ret;
  4838. }
  4839. struct nfs4_get_lease_time_data {
  4840. struct nfs4_get_lease_time_args *args;
  4841. struct nfs4_get_lease_time_res *res;
  4842. struct nfs_client *clp;
  4843. };
  4844. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  4845. void *calldata)
  4846. {
  4847. struct nfs4_get_lease_time_data *data =
  4848. (struct nfs4_get_lease_time_data *)calldata;
  4849. dprintk("--> %s\n", __func__);
  4850. /* just setup sequence, do not trigger session recovery
  4851. since we're invoked within one */
  4852. nfs41_setup_sequence(data->clp->cl_session,
  4853. &data->args->la_seq_args,
  4854. &data->res->lr_seq_res,
  4855. task);
  4856. dprintk("<-- %s\n", __func__);
  4857. }
  4858. /*
  4859. * Called from nfs4_state_manager thread for session setup, so don't recover
  4860. * from sequence operation or clientid errors.
  4861. */
  4862. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  4863. {
  4864. struct nfs4_get_lease_time_data *data =
  4865. (struct nfs4_get_lease_time_data *)calldata;
  4866. dprintk("--> %s\n", __func__);
  4867. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  4868. return;
  4869. switch (task->tk_status) {
  4870. case -NFS4ERR_DELAY:
  4871. case -NFS4ERR_GRACE:
  4872. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  4873. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  4874. task->tk_status = 0;
  4875. /* fall through */
  4876. case -NFS4ERR_RETRY_UNCACHED_REP:
  4877. rpc_restart_call_prepare(task);
  4878. return;
  4879. }
  4880. dprintk("<-- %s\n", __func__);
  4881. }
  4882. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  4883. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  4884. .rpc_call_done = nfs4_get_lease_time_done,
  4885. };
  4886. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  4887. {
  4888. struct rpc_task *task;
  4889. struct nfs4_get_lease_time_args args;
  4890. struct nfs4_get_lease_time_res res = {
  4891. .lr_fsinfo = fsinfo,
  4892. };
  4893. struct nfs4_get_lease_time_data data = {
  4894. .args = &args,
  4895. .res = &res,
  4896. .clp = clp,
  4897. };
  4898. struct rpc_message msg = {
  4899. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  4900. .rpc_argp = &args,
  4901. .rpc_resp = &res,
  4902. };
  4903. struct rpc_task_setup task_setup = {
  4904. .rpc_client = clp->cl_rpcclient,
  4905. .rpc_message = &msg,
  4906. .callback_ops = &nfs4_get_lease_time_ops,
  4907. .callback_data = &data,
  4908. .flags = RPC_TASK_TIMEOUT,
  4909. };
  4910. int status;
  4911. nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
  4912. nfs4_set_sequence_privileged(&args.la_seq_args);
  4913. dprintk("--> %s\n", __func__);
  4914. task = rpc_run_task(&task_setup);
  4915. if (IS_ERR(task))
  4916. status = PTR_ERR(task);
  4917. else {
  4918. status = task->tk_status;
  4919. rpc_put_task(task);
  4920. }
  4921. dprintk("<-- %s return %d\n", __func__, status);
  4922. return status;
  4923. }
  4924. /*
  4925. * Initialize the values to be used by the client in CREATE_SESSION
  4926. * If nfs4_init_session set the fore channel request and response sizes,
  4927. * use them.
  4928. *
  4929. * Set the back channel max_resp_sz_cached to zero to force the client to
  4930. * always set csa_cachethis to FALSE because the current implementation
  4931. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  4932. */
  4933. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  4934. {
  4935. struct nfs4_session *session = args->client->cl_session;
  4936. unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
  4937. mxresp_sz = session->fc_target_max_resp_sz;
  4938. if (mxrqst_sz == 0)
  4939. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  4940. if (mxresp_sz == 0)
  4941. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  4942. /* Fore channel attributes */
  4943. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  4944. args->fc_attrs.max_resp_sz = mxresp_sz;
  4945. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  4946. args->fc_attrs.max_reqs = max_session_slots;
  4947. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  4948. "max_ops=%u max_reqs=%u\n",
  4949. __func__,
  4950. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  4951. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  4952. /* Back channel attributes */
  4953. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  4954. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  4955. args->bc_attrs.max_resp_sz_cached = 0;
  4956. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  4957. args->bc_attrs.max_reqs = 1;
  4958. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  4959. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4960. __func__,
  4961. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  4962. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  4963. args->bc_attrs.max_reqs);
  4964. }
  4965. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  4966. {
  4967. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  4968. struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
  4969. if (rcvd->max_resp_sz > sent->max_resp_sz)
  4970. return -EINVAL;
  4971. /*
  4972. * Our requested max_ops is the minimum we need; we're not
  4973. * prepared to break up compounds into smaller pieces than that.
  4974. * So, no point even trying to continue if the server won't
  4975. * cooperate:
  4976. */
  4977. if (rcvd->max_ops < sent->max_ops)
  4978. return -EINVAL;
  4979. if (rcvd->max_reqs == 0)
  4980. return -EINVAL;
  4981. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  4982. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  4983. return 0;
  4984. }
  4985. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  4986. {
  4987. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  4988. struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
  4989. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  4990. return -EINVAL;
  4991. if (rcvd->max_resp_sz < sent->max_resp_sz)
  4992. return -EINVAL;
  4993. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  4994. return -EINVAL;
  4995. /* These would render the backchannel useless: */
  4996. if (rcvd->max_ops != sent->max_ops)
  4997. return -EINVAL;
  4998. if (rcvd->max_reqs != sent->max_reqs)
  4999. return -EINVAL;
  5000. return 0;
  5001. }
  5002. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  5003. struct nfs4_session *session)
  5004. {
  5005. int ret;
  5006. ret = nfs4_verify_fore_channel_attrs(args, session);
  5007. if (ret)
  5008. return ret;
  5009. return nfs4_verify_back_channel_attrs(args, session);
  5010. }
  5011. static int _nfs4_proc_create_session(struct nfs_client *clp,
  5012. struct rpc_cred *cred)
  5013. {
  5014. struct nfs4_session *session = clp->cl_session;
  5015. struct nfs41_create_session_args args = {
  5016. .client = clp,
  5017. .cb_program = NFS4_CALLBACK,
  5018. };
  5019. struct nfs41_create_session_res res = {
  5020. .client = clp,
  5021. };
  5022. struct rpc_message msg = {
  5023. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  5024. .rpc_argp = &args,
  5025. .rpc_resp = &res,
  5026. .rpc_cred = cred,
  5027. };
  5028. int status;
  5029. nfs4_init_channel_attrs(&args);
  5030. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  5031. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5032. if (!status) {
  5033. /* Verify the session's negotiated channel_attrs values */
  5034. status = nfs4_verify_channel_attrs(&args, session);
  5035. /* Increment the clientid slot sequence id */
  5036. clp->cl_seqid++;
  5037. }
  5038. return status;
  5039. }
  5040. /*
  5041. * Issues a CREATE_SESSION operation to the server.
  5042. * It is the responsibility of the caller to verify the session is
  5043. * expired before calling this routine.
  5044. */
  5045. int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
  5046. {
  5047. int status;
  5048. unsigned *ptr;
  5049. struct nfs4_session *session = clp->cl_session;
  5050. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  5051. status = _nfs4_proc_create_session(clp, cred);
  5052. if (status)
  5053. goto out;
  5054. /* Init or reset the session slot tables */
  5055. status = nfs4_setup_session_slot_tables(session);
  5056. dprintk("slot table setup returned %d\n", status);
  5057. if (status)
  5058. goto out;
  5059. ptr = (unsigned *)&session->sess_id.data[0];
  5060. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  5061. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  5062. out:
  5063. dprintk("<-- %s\n", __func__);
  5064. return status;
  5065. }
  5066. /*
  5067. * Issue the over-the-wire RPC DESTROY_SESSION.
  5068. * The caller must serialize access to this routine.
  5069. */
  5070. int nfs4_proc_destroy_session(struct nfs4_session *session,
  5071. struct rpc_cred *cred)
  5072. {
  5073. struct rpc_message msg = {
  5074. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  5075. .rpc_argp = session,
  5076. .rpc_cred = cred,
  5077. };
  5078. int status = 0;
  5079. dprintk("--> nfs4_proc_destroy_session\n");
  5080. /* session is still being setup */
  5081. if (session->clp->cl_cons_state != NFS_CS_READY)
  5082. return status;
  5083. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5084. if (status)
  5085. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  5086. "Session has been destroyed regardless...\n", status);
  5087. dprintk("<-- nfs4_proc_destroy_session\n");
  5088. return status;
  5089. }
  5090. /*
  5091. * Renew the cl_session lease.
  5092. */
  5093. struct nfs4_sequence_data {
  5094. struct nfs_client *clp;
  5095. struct nfs4_sequence_args args;
  5096. struct nfs4_sequence_res res;
  5097. };
  5098. static void nfs41_sequence_release(void *data)
  5099. {
  5100. struct nfs4_sequence_data *calldata = data;
  5101. struct nfs_client *clp = calldata->clp;
  5102. if (atomic_read(&clp->cl_count) > 1)
  5103. nfs4_schedule_state_renewal(clp);
  5104. nfs_put_client(clp);
  5105. kfree(calldata);
  5106. }
  5107. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5108. {
  5109. switch(task->tk_status) {
  5110. case -NFS4ERR_DELAY:
  5111. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5112. return -EAGAIN;
  5113. default:
  5114. nfs4_schedule_lease_recovery(clp);
  5115. }
  5116. return 0;
  5117. }
  5118. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  5119. {
  5120. struct nfs4_sequence_data *calldata = data;
  5121. struct nfs_client *clp = calldata->clp;
  5122. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  5123. return;
  5124. if (task->tk_status < 0) {
  5125. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  5126. if (atomic_read(&clp->cl_count) == 1)
  5127. goto out;
  5128. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  5129. rpc_restart_call_prepare(task);
  5130. return;
  5131. }
  5132. }
  5133. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  5134. out:
  5135. dprintk("<-- %s\n", __func__);
  5136. }
  5137. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  5138. {
  5139. struct nfs4_sequence_data *calldata = data;
  5140. struct nfs_client *clp = calldata->clp;
  5141. struct nfs4_sequence_args *args;
  5142. struct nfs4_sequence_res *res;
  5143. args = task->tk_msg.rpc_argp;
  5144. res = task->tk_msg.rpc_resp;
  5145. nfs41_setup_sequence(clp->cl_session, args, res, task);
  5146. }
  5147. static const struct rpc_call_ops nfs41_sequence_ops = {
  5148. .rpc_call_done = nfs41_sequence_call_done,
  5149. .rpc_call_prepare = nfs41_sequence_prepare,
  5150. .rpc_release = nfs41_sequence_release,
  5151. };
  5152. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  5153. struct rpc_cred *cred,
  5154. bool is_privileged)
  5155. {
  5156. struct nfs4_sequence_data *calldata;
  5157. struct rpc_message msg = {
  5158. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  5159. .rpc_cred = cred,
  5160. };
  5161. struct rpc_task_setup task_setup_data = {
  5162. .rpc_client = clp->cl_rpcclient,
  5163. .rpc_message = &msg,
  5164. .callback_ops = &nfs41_sequence_ops,
  5165. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
  5166. };
  5167. if (!atomic_inc_not_zero(&clp->cl_count))
  5168. return ERR_PTR(-EIO);
  5169. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5170. if (calldata == NULL) {
  5171. nfs_put_client(clp);
  5172. return ERR_PTR(-ENOMEM);
  5173. }
  5174. nfs41_init_sequence(&calldata->args, &calldata->res, 0);
  5175. if (is_privileged)
  5176. nfs4_set_sequence_privileged(&calldata->args);
  5177. msg.rpc_argp = &calldata->args;
  5178. msg.rpc_resp = &calldata->res;
  5179. calldata->clp = clp;
  5180. task_setup_data.callback_data = calldata;
  5181. return rpc_run_task(&task_setup_data);
  5182. }
  5183. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  5184. {
  5185. struct rpc_task *task;
  5186. int ret = 0;
  5187. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  5188. return 0;
  5189. task = _nfs41_proc_sequence(clp, cred, false);
  5190. if (IS_ERR(task))
  5191. ret = PTR_ERR(task);
  5192. else
  5193. rpc_put_task_async(task);
  5194. dprintk("<-- %s status=%d\n", __func__, ret);
  5195. return ret;
  5196. }
  5197. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  5198. {
  5199. struct rpc_task *task;
  5200. int ret;
  5201. task = _nfs41_proc_sequence(clp, cred, true);
  5202. if (IS_ERR(task)) {
  5203. ret = PTR_ERR(task);
  5204. goto out;
  5205. }
  5206. ret = rpc_wait_for_completion_task(task);
  5207. if (!ret) {
  5208. struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
  5209. if (task->tk_status == 0)
  5210. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
  5211. ret = task->tk_status;
  5212. }
  5213. rpc_put_task(task);
  5214. out:
  5215. dprintk("<-- %s status=%d\n", __func__, ret);
  5216. return ret;
  5217. }
  5218. struct nfs4_reclaim_complete_data {
  5219. struct nfs_client *clp;
  5220. struct nfs41_reclaim_complete_args arg;
  5221. struct nfs41_reclaim_complete_res res;
  5222. };
  5223. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  5224. {
  5225. struct nfs4_reclaim_complete_data *calldata = data;
  5226. nfs41_setup_sequence(calldata->clp->cl_session,
  5227. &calldata->arg.seq_args,
  5228. &calldata->res.seq_res,
  5229. task);
  5230. }
  5231. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5232. {
  5233. switch(task->tk_status) {
  5234. case 0:
  5235. case -NFS4ERR_COMPLETE_ALREADY:
  5236. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  5237. break;
  5238. case -NFS4ERR_DELAY:
  5239. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5240. /* fall through */
  5241. case -NFS4ERR_RETRY_UNCACHED_REP:
  5242. return -EAGAIN;
  5243. default:
  5244. nfs4_schedule_lease_recovery(clp);
  5245. }
  5246. return 0;
  5247. }
  5248. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  5249. {
  5250. struct nfs4_reclaim_complete_data *calldata = data;
  5251. struct nfs_client *clp = calldata->clp;
  5252. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  5253. dprintk("--> %s\n", __func__);
  5254. if (!nfs41_sequence_done(task, res))
  5255. return;
  5256. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  5257. rpc_restart_call_prepare(task);
  5258. return;
  5259. }
  5260. dprintk("<-- %s\n", __func__);
  5261. }
  5262. static void nfs4_free_reclaim_complete_data(void *data)
  5263. {
  5264. struct nfs4_reclaim_complete_data *calldata = data;
  5265. kfree(calldata);
  5266. }
  5267. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  5268. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  5269. .rpc_call_done = nfs4_reclaim_complete_done,
  5270. .rpc_release = nfs4_free_reclaim_complete_data,
  5271. };
  5272. /*
  5273. * Issue a global reclaim complete.
  5274. */
  5275. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  5276. {
  5277. struct nfs4_reclaim_complete_data *calldata;
  5278. struct rpc_task *task;
  5279. struct rpc_message msg = {
  5280. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  5281. };
  5282. struct rpc_task_setup task_setup_data = {
  5283. .rpc_client = clp->cl_rpcclient,
  5284. .rpc_message = &msg,
  5285. .callback_ops = &nfs4_reclaim_complete_call_ops,
  5286. .flags = RPC_TASK_ASYNC,
  5287. };
  5288. int status = -ENOMEM;
  5289. dprintk("--> %s\n", __func__);
  5290. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5291. if (calldata == NULL)
  5292. goto out;
  5293. calldata->clp = clp;
  5294. calldata->arg.one_fs = 0;
  5295. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
  5296. nfs4_set_sequence_privileged(&calldata->arg.seq_args);
  5297. msg.rpc_argp = &calldata->arg;
  5298. msg.rpc_resp = &calldata->res;
  5299. task_setup_data.callback_data = calldata;
  5300. task = rpc_run_task(&task_setup_data);
  5301. if (IS_ERR(task)) {
  5302. status = PTR_ERR(task);
  5303. goto out;
  5304. }
  5305. status = nfs4_wait_for_completion_rpc_task(task);
  5306. if (status == 0)
  5307. status = task->tk_status;
  5308. rpc_put_task(task);
  5309. return 0;
  5310. out:
  5311. dprintk("<-- %s status=%d\n", __func__, status);
  5312. return status;
  5313. }
  5314. static void
  5315. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  5316. {
  5317. struct nfs4_layoutget *lgp = calldata;
  5318. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5319. struct nfs4_session *session = nfs4_get_session(server);
  5320. dprintk("--> %s\n", __func__);
  5321. /* Note the is a race here, where a CB_LAYOUTRECALL can come in
  5322. * right now covering the LAYOUTGET we are about to send.
  5323. * However, that is not so catastrophic, and there seems
  5324. * to be no way to prevent it completely.
  5325. */
  5326. if (nfs41_setup_sequence(session, &lgp->args.seq_args,
  5327. &lgp->res.seq_res, task))
  5328. return;
  5329. if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
  5330. NFS_I(lgp->args.inode)->layout,
  5331. lgp->args.ctx->state)) {
  5332. rpc_exit(task, NFS4_OK);
  5333. }
  5334. }
  5335. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  5336. {
  5337. struct nfs4_layoutget *lgp = calldata;
  5338. struct inode *inode = lgp->args.inode;
  5339. struct nfs_server *server = NFS_SERVER(inode);
  5340. struct pnfs_layout_hdr *lo;
  5341. struct nfs4_state *state = NULL;
  5342. dprintk("--> %s\n", __func__);
  5343. if (!nfs41_sequence_done(task, &lgp->res.seq_res))
  5344. goto out;
  5345. switch (task->tk_status) {
  5346. case 0:
  5347. goto out;
  5348. case -NFS4ERR_LAYOUTTRYLATER:
  5349. case -NFS4ERR_RECALLCONFLICT:
  5350. task->tk_status = -NFS4ERR_DELAY;
  5351. break;
  5352. case -NFS4ERR_EXPIRED:
  5353. case -NFS4ERR_BAD_STATEID:
  5354. spin_lock(&inode->i_lock);
  5355. lo = NFS_I(inode)->layout;
  5356. if (!lo || list_empty(&lo->plh_segs)) {
  5357. spin_unlock(&inode->i_lock);
  5358. /* If the open stateid was bad, then recover it. */
  5359. state = lgp->args.ctx->state;
  5360. } else {
  5361. LIST_HEAD(head);
  5362. pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
  5363. spin_unlock(&inode->i_lock);
  5364. /* Mark the bad layout state as invalid, then
  5365. * retry using the open stateid. */
  5366. pnfs_free_lseg_list(&head);
  5367. }
  5368. }
  5369. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  5370. rpc_restart_call_prepare(task);
  5371. out:
  5372. dprintk("<-- %s\n", __func__);
  5373. }
  5374. static size_t max_response_pages(struct nfs_server *server)
  5375. {
  5376. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  5377. return nfs_page_array_len(0, max_resp_sz);
  5378. }
  5379. static void nfs4_free_pages(struct page **pages, size_t size)
  5380. {
  5381. int i;
  5382. if (!pages)
  5383. return;
  5384. for (i = 0; i < size; i++) {
  5385. if (!pages[i])
  5386. break;
  5387. __free_page(pages[i]);
  5388. }
  5389. kfree(pages);
  5390. }
  5391. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  5392. {
  5393. struct page **pages;
  5394. int i;
  5395. pages = kcalloc(size, sizeof(struct page *), gfp_flags);
  5396. if (!pages) {
  5397. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  5398. return NULL;
  5399. }
  5400. for (i = 0; i < size; i++) {
  5401. pages[i] = alloc_page(gfp_flags);
  5402. if (!pages[i]) {
  5403. dprintk("%s: failed to allocate page\n", __func__);
  5404. nfs4_free_pages(pages, size);
  5405. return NULL;
  5406. }
  5407. }
  5408. return pages;
  5409. }
  5410. static void nfs4_layoutget_release(void *calldata)
  5411. {
  5412. struct nfs4_layoutget *lgp = calldata;
  5413. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5414. size_t max_pages = max_response_pages(server);
  5415. dprintk("--> %s\n", __func__);
  5416. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  5417. put_nfs_open_context(lgp->args.ctx);
  5418. kfree(calldata);
  5419. dprintk("<-- %s\n", __func__);
  5420. }
  5421. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  5422. .rpc_call_prepare = nfs4_layoutget_prepare,
  5423. .rpc_call_done = nfs4_layoutget_done,
  5424. .rpc_release = nfs4_layoutget_release,
  5425. };
  5426. struct pnfs_layout_segment *
  5427. nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
  5428. {
  5429. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5430. size_t max_pages = max_response_pages(server);
  5431. struct rpc_task *task;
  5432. struct rpc_message msg = {
  5433. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  5434. .rpc_argp = &lgp->args,
  5435. .rpc_resp = &lgp->res,
  5436. };
  5437. struct rpc_task_setup task_setup_data = {
  5438. .rpc_client = server->client,
  5439. .rpc_message = &msg,
  5440. .callback_ops = &nfs4_layoutget_call_ops,
  5441. .callback_data = lgp,
  5442. .flags = RPC_TASK_ASYNC,
  5443. };
  5444. struct pnfs_layout_segment *lseg = NULL;
  5445. int status = 0;
  5446. dprintk("--> %s\n", __func__);
  5447. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  5448. if (!lgp->args.layout.pages) {
  5449. nfs4_layoutget_release(lgp);
  5450. return ERR_PTR(-ENOMEM);
  5451. }
  5452. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  5453. lgp->res.layoutp = &lgp->args.layout;
  5454. lgp->res.seq_res.sr_slot = NULL;
  5455. nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
  5456. task = rpc_run_task(&task_setup_data);
  5457. if (IS_ERR(task))
  5458. return ERR_CAST(task);
  5459. status = nfs4_wait_for_completion_rpc_task(task);
  5460. if (status == 0)
  5461. status = task->tk_status;
  5462. if (status == 0)
  5463. lseg = pnfs_layout_process(lgp);
  5464. rpc_put_task(task);
  5465. dprintk("<-- %s status=%d\n", __func__, status);
  5466. if (status)
  5467. return ERR_PTR(status);
  5468. return lseg;
  5469. }
  5470. static void
  5471. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  5472. {
  5473. struct nfs4_layoutreturn *lrp = calldata;
  5474. dprintk("--> %s\n", __func__);
  5475. nfs41_setup_sequence(lrp->clp->cl_session,
  5476. &lrp->args.seq_args,
  5477. &lrp->res.seq_res,
  5478. task);
  5479. }
  5480. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  5481. {
  5482. struct nfs4_layoutreturn *lrp = calldata;
  5483. struct nfs_server *server;
  5484. dprintk("--> %s\n", __func__);
  5485. if (!nfs41_sequence_done(task, &lrp->res.seq_res))
  5486. return;
  5487. server = NFS_SERVER(lrp->args.inode);
  5488. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5489. rpc_restart_call_prepare(task);
  5490. return;
  5491. }
  5492. dprintk("<-- %s\n", __func__);
  5493. }
  5494. static void nfs4_layoutreturn_release(void *calldata)
  5495. {
  5496. struct nfs4_layoutreturn *lrp = calldata;
  5497. struct pnfs_layout_hdr *lo = lrp->args.layout;
  5498. dprintk("--> %s\n", __func__);
  5499. spin_lock(&lo->plh_inode->i_lock);
  5500. if (lrp->res.lrs_present)
  5501. pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
  5502. lo->plh_block_lgets--;
  5503. spin_unlock(&lo->plh_inode->i_lock);
  5504. pnfs_put_layout_hdr(lrp->args.layout);
  5505. kfree(calldata);
  5506. dprintk("<-- %s\n", __func__);
  5507. }
  5508. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  5509. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  5510. .rpc_call_done = nfs4_layoutreturn_done,
  5511. .rpc_release = nfs4_layoutreturn_release,
  5512. };
  5513. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
  5514. {
  5515. struct rpc_task *task;
  5516. struct rpc_message msg = {
  5517. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  5518. .rpc_argp = &lrp->args,
  5519. .rpc_resp = &lrp->res,
  5520. };
  5521. struct rpc_task_setup task_setup_data = {
  5522. .rpc_client = lrp->clp->cl_rpcclient,
  5523. .rpc_message = &msg,
  5524. .callback_ops = &nfs4_layoutreturn_call_ops,
  5525. .callback_data = lrp,
  5526. };
  5527. int status;
  5528. dprintk("--> %s\n", __func__);
  5529. nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
  5530. task = rpc_run_task(&task_setup_data);
  5531. if (IS_ERR(task))
  5532. return PTR_ERR(task);
  5533. status = task->tk_status;
  5534. dprintk("<-- %s status=%d\n", __func__, status);
  5535. rpc_put_task(task);
  5536. return status;
  5537. }
  5538. /*
  5539. * Retrieve the list of Data Server devices from the MDS.
  5540. */
  5541. static int _nfs4_getdevicelist(struct nfs_server *server,
  5542. const struct nfs_fh *fh,
  5543. struct pnfs_devicelist *devlist)
  5544. {
  5545. struct nfs4_getdevicelist_args args = {
  5546. .fh = fh,
  5547. .layoutclass = server->pnfs_curr_ld->id,
  5548. };
  5549. struct nfs4_getdevicelist_res res = {
  5550. .devlist = devlist,
  5551. };
  5552. struct rpc_message msg = {
  5553. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
  5554. .rpc_argp = &args,
  5555. .rpc_resp = &res,
  5556. };
  5557. int status;
  5558. dprintk("--> %s\n", __func__);
  5559. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
  5560. &res.seq_res, 0);
  5561. dprintk("<-- %s status=%d\n", __func__, status);
  5562. return status;
  5563. }
  5564. int nfs4_proc_getdevicelist(struct nfs_server *server,
  5565. const struct nfs_fh *fh,
  5566. struct pnfs_devicelist *devlist)
  5567. {
  5568. struct nfs4_exception exception = { };
  5569. int err;
  5570. do {
  5571. err = nfs4_handle_exception(server,
  5572. _nfs4_getdevicelist(server, fh, devlist),
  5573. &exception);
  5574. } while (exception.retry);
  5575. dprintk("%s: err=%d, num_devs=%u\n", __func__,
  5576. err, devlist->num_devs);
  5577. return err;
  5578. }
  5579. EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
  5580. static int
  5581. _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5582. {
  5583. struct nfs4_getdeviceinfo_args args = {
  5584. .pdev = pdev,
  5585. };
  5586. struct nfs4_getdeviceinfo_res res = {
  5587. .pdev = pdev,
  5588. };
  5589. struct rpc_message msg = {
  5590. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  5591. .rpc_argp = &args,
  5592. .rpc_resp = &res,
  5593. };
  5594. int status;
  5595. dprintk("--> %s\n", __func__);
  5596. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5597. dprintk("<-- %s status=%d\n", __func__, status);
  5598. return status;
  5599. }
  5600. int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5601. {
  5602. struct nfs4_exception exception = { };
  5603. int err;
  5604. do {
  5605. err = nfs4_handle_exception(server,
  5606. _nfs4_proc_getdeviceinfo(server, pdev),
  5607. &exception);
  5608. } while (exception.retry);
  5609. return err;
  5610. }
  5611. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  5612. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  5613. {
  5614. struct nfs4_layoutcommit_data *data = calldata;
  5615. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5616. struct nfs4_session *session = nfs4_get_session(server);
  5617. nfs41_setup_sequence(session,
  5618. &data->args.seq_args,
  5619. &data->res.seq_res,
  5620. task);
  5621. }
  5622. static void
  5623. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  5624. {
  5625. struct nfs4_layoutcommit_data *data = calldata;
  5626. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5627. if (!nfs41_sequence_done(task, &data->res.seq_res))
  5628. return;
  5629. switch (task->tk_status) { /* Just ignore these failures */
  5630. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  5631. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  5632. case -NFS4ERR_BADLAYOUT: /* no layout */
  5633. case -NFS4ERR_GRACE: /* loca_recalim always false */
  5634. task->tk_status = 0;
  5635. break;
  5636. case 0:
  5637. nfs_post_op_update_inode_force_wcc(data->args.inode,
  5638. data->res.fattr);
  5639. break;
  5640. default:
  5641. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5642. rpc_restart_call_prepare(task);
  5643. return;
  5644. }
  5645. }
  5646. }
  5647. static void nfs4_layoutcommit_release(void *calldata)
  5648. {
  5649. struct nfs4_layoutcommit_data *data = calldata;
  5650. struct pnfs_layout_segment *lseg, *tmp;
  5651. unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
  5652. pnfs_cleanup_layoutcommit(data);
  5653. /* Matched by references in pnfs_set_layoutcommit */
  5654. list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
  5655. list_del_init(&lseg->pls_lc_list);
  5656. if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
  5657. &lseg->pls_flags))
  5658. pnfs_put_lseg(lseg);
  5659. }
  5660. clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
  5661. smp_mb__after_clear_bit();
  5662. wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
  5663. put_rpccred(data->cred);
  5664. kfree(data);
  5665. }
  5666. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  5667. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  5668. .rpc_call_done = nfs4_layoutcommit_done,
  5669. .rpc_release = nfs4_layoutcommit_release,
  5670. };
  5671. int
  5672. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  5673. {
  5674. struct rpc_message msg = {
  5675. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  5676. .rpc_argp = &data->args,
  5677. .rpc_resp = &data->res,
  5678. .rpc_cred = data->cred,
  5679. };
  5680. struct rpc_task_setup task_setup_data = {
  5681. .task = &data->task,
  5682. .rpc_client = NFS_CLIENT(data->args.inode),
  5683. .rpc_message = &msg,
  5684. .callback_ops = &nfs4_layoutcommit_ops,
  5685. .callback_data = data,
  5686. .flags = RPC_TASK_ASYNC,
  5687. };
  5688. struct rpc_task *task;
  5689. int status = 0;
  5690. dprintk("NFS: %4d initiating layoutcommit call. sync %d "
  5691. "lbw: %llu inode %lu\n",
  5692. data->task.tk_pid, sync,
  5693. data->args.lastbytewritten,
  5694. data->args.inode->i_ino);
  5695. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  5696. task = rpc_run_task(&task_setup_data);
  5697. if (IS_ERR(task))
  5698. return PTR_ERR(task);
  5699. if (sync == false)
  5700. goto out;
  5701. status = nfs4_wait_for_completion_rpc_task(task);
  5702. if (status != 0)
  5703. goto out;
  5704. status = task->tk_status;
  5705. out:
  5706. dprintk("%s: status %d\n", __func__, status);
  5707. rpc_put_task(task);
  5708. return status;
  5709. }
  5710. static int
  5711. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5712. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5713. {
  5714. struct nfs41_secinfo_no_name_args args = {
  5715. .style = SECINFO_STYLE_CURRENT_FH,
  5716. };
  5717. struct nfs4_secinfo_res res = {
  5718. .flavors = flavors,
  5719. };
  5720. struct rpc_message msg = {
  5721. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  5722. .rpc_argp = &args,
  5723. .rpc_resp = &res,
  5724. };
  5725. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5726. }
  5727. static int
  5728. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5729. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5730. {
  5731. struct nfs4_exception exception = { };
  5732. int err;
  5733. do {
  5734. err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5735. switch (err) {
  5736. case 0:
  5737. case -NFS4ERR_WRONGSEC:
  5738. case -NFS4ERR_NOTSUPP:
  5739. goto out;
  5740. default:
  5741. err = nfs4_handle_exception(server, err, &exception);
  5742. }
  5743. } while (exception.retry);
  5744. out:
  5745. return err;
  5746. }
  5747. static int
  5748. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  5749. struct nfs_fsinfo *info)
  5750. {
  5751. int err;
  5752. struct page *page;
  5753. rpc_authflavor_t flavor;
  5754. struct nfs4_secinfo_flavors *flavors;
  5755. page = alloc_page(GFP_KERNEL);
  5756. if (!page) {
  5757. err = -ENOMEM;
  5758. goto out;
  5759. }
  5760. flavors = page_address(page);
  5761. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5762. /*
  5763. * Fall back on "guess and check" method if
  5764. * the server doesn't support SECINFO_NO_NAME
  5765. */
  5766. if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
  5767. err = nfs4_find_root_sec(server, fhandle, info);
  5768. goto out_freepage;
  5769. }
  5770. if (err)
  5771. goto out_freepage;
  5772. flavor = nfs_find_best_sec(flavors);
  5773. if (err == 0)
  5774. err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
  5775. out_freepage:
  5776. put_page(page);
  5777. if (err == -EACCES)
  5778. return -EPERM;
  5779. out:
  5780. return err;
  5781. }
  5782. static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5783. {
  5784. int status;
  5785. struct nfs41_test_stateid_args args = {
  5786. .stateid = stateid,
  5787. };
  5788. struct nfs41_test_stateid_res res;
  5789. struct rpc_message msg = {
  5790. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  5791. .rpc_argp = &args,
  5792. .rpc_resp = &res,
  5793. };
  5794. dprintk("NFS call test_stateid %p\n", stateid);
  5795. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  5796. nfs4_set_sequence_privileged(&args.seq_args);
  5797. status = nfs4_call_sync_sequence(server->client, server, &msg,
  5798. &args.seq_args, &res.seq_res);
  5799. if (status != NFS_OK) {
  5800. dprintk("NFS reply test_stateid: failed, %d\n", status);
  5801. return status;
  5802. }
  5803. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  5804. return -res.status;
  5805. }
  5806. /**
  5807. * nfs41_test_stateid - perform a TEST_STATEID operation
  5808. *
  5809. * @server: server / transport on which to perform the operation
  5810. * @stateid: state ID to test
  5811. *
  5812. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  5813. * Otherwise a negative NFS4ERR value is returned if the operation
  5814. * failed or the state ID is not currently valid.
  5815. */
  5816. static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5817. {
  5818. struct nfs4_exception exception = { };
  5819. int err;
  5820. do {
  5821. err = _nfs41_test_stateid(server, stateid);
  5822. if (err != -NFS4ERR_DELAY)
  5823. break;
  5824. nfs4_handle_exception(server, err, &exception);
  5825. } while (exception.retry);
  5826. return err;
  5827. }
  5828. static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5829. {
  5830. struct nfs41_free_stateid_args args = {
  5831. .stateid = stateid,
  5832. };
  5833. struct nfs41_free_stateid_res res;
  5834. struct rpc_message msg = {
  5835. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  5836. .rpc_argp = &args,
  5837. .rpc_resp = &res,
  5838. };
  5839. int status;
  5840. dprintk("NFS call free_stateid %p\n", stateid);
  5841. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  5842. nfs4_set_sequence_privileged(&args.seq_args);
  5843. status = nfs4_call_sync_sequence(server->client, server, &msg,
  5844. &args.seq_args, &res.seq_res);
  5845. dprintk("NFS reply free_stateid: %d\n", status);
  5846. return status;
  5847. }
  5848. /**
  5849. * nfs41_free_stateid - perform a FREE_STATEID operation
  5850. *
  5851. * @server: server / transport on which to perform the operation
  5852. * @stateid: state ID to release
  5853. *
  5854. * Returns NFS_OK if the server freed "stateid". Otherwise a
  5855. * negative NFS4ERR value is returned.
  5856. */
  5857. static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5858. {
  5859. struct nfs4_exception exception = { };
  5860. int err;
  5861. do {
  5862. err = _nfs4_free_stateid(server, stateid);
  5863. if (err != -NFS4ERR_DELAY)
  5864. break;
  5865. nfs4_handle_exception(server, err, &exception);
  5866. } while (exception.retry);
  5867. return err;
  5868. }
  5869. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  5870. const nfs4_stateid *s2)
  5871. {
  5872. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  5873. return false;
  5874. if (s1->seqid == s2->seqid)
  5875. return true;
  5876. if (s1->seqid == 0 || s2->seqid == 0)
  5877. return true;
  5878. return false;
  5879. }
  5880. #endif /* CONFIG_NFS_V4_1 */
  5881. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  5882. const nfs4_stateid *s2)
  5883. {
  5884. return nfs4_stateid_match(s1, s2);
  5885. }
  5886. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  5887. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  5888. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  5889. .recover_open = nfs4_open_reclaim,
  5890. .recover_lock = nfs4_lock_reclaim,
  5891. .establish_clid = nfs4_init_clientid,
  5892. .get_clid_cred = nfs4_get_setclientid_cred,
  5893. .detect_trunking = nfs40_discover_server_trunking,
  5894. };
  5895. #if defined(CONFIG_NFS_V4_1)
  5896. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  5897. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  5898. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  5899. .recover_open = nfs4_open_reclaim,
  5900. .recover_lock = nfs4_lock_reclaim,
  5901. .establish_clid = nfs41_init_clientid,
  5902. .get_clid_cred = nfs4_get_exchange_id_cred,
  5903. .reclaim_complete = nfs41_proc_reclaim_complete,
  5904. .detect_trunking = nfs41_discover_server_trunking,
  5905. };
  5906. #endif /* CONFIG_NFS_V4_1 */
  5907. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  5908. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  5909. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  5910. .recover_open = nfs4_open_expired,
  5911. .recover_lock = nfs4_lock_expired,
  5912. .establish_clid = nfs4_init_clientid,
  5913. .get_clid_cred = nfs4_get_setclientid_cred,
  5914. };
  5915. #if defined(CONFIG_NFS_V4_1)
  5916. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  5917. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  5918. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  5919. .recover_open = nfs41_open_expired,
  5920. .recover_lock = nfs41_lock_expired,
  5921. .establish_clid = nfs41_init_clientid,
  5922. .get_clid_cred = nfs4_get_exchange_id_cred,
  5923. };
  5924. #endif /* CONFIG_NFS_V4_1 */
  5925. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  5926. .sched_state_renewal = nfs4_proc_async_renew,
  5927. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  5928. .renew_lease = nfs4_proc_renew,
  5929. };
  5930. #if defined(CONFIG_NFS_V4_1)
  5931. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  5932. .sched_state_renewal = nfs41_proc_async_sequence,
  5933. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  5934. .renew_lease = nfs4_proc_sequence,
  5935. };
  5936. #endif
  5937. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  5938. .minor_version = 0,
  5939. .call_sync = _nfs4_call_sync,
  5940. .match_stateid = nfs4_match_stateid,
  5941. .find_root_sec = nfs4_find_root_sec,
  5942. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  5943. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  5944. .state_renewal_ops = &nfs40_state_renewal_ops,
  5945. };
  5946. #if defined(CONFIG_NFS_V4_1)
  5947. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  5948. .minor_version = 1,
  5949. .call_sync = nfs4_call_sync_sequence,
  5950. .match_stateid = nfs41_match_stateid,
  5951. .find_root_sec = nfs41_find_root_sec,
  5952. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  5953. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  5954. .state_renewal_ops = &nfs41_state_renewal_ops,
  5955. };
  5956. #endif
  5957. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  5958. [0] = &nfs_v4_0_minor_ops,
  5959. #if defined(CONFIG_NFS_V4_1)
  5960. [1] = &nfs_v4_1_minor_ops,
  5961. #endif
  5962. };
  5963. const struct inode_operations nfs4_dir_inode_operations = {
  5964. .create = nfs_create,
  5965. .lookup = nfs_lookup,
  5966. .atomic_open = nfs_atomic_open,
  5967. .link = nfs_link,
  5968. .unlink = nfs_unlink,
  5969. .symlink = nfs_symlink,
  5970. .mkdir = nfs_mkdir,
  5971. .rmdir = nfs_rmdir,
  5972. .mknod = nfs_mknod,
  5973. .rename = nfs_rename,
  5974. .permission = nfs_permission,
  5975. .getattr = nfs_getattr,
  5976. .setattr = nfs_setattr,
  5977. .getxattr = generic_getxattr,
  5978. .setxattr = generic_setxattr,
  5979. .listxattr = generic_listxattr,
  5980. .removexattr = generic_removexattr,
  5981. };
  5982. static const struct inode_operations nfs4_file_inode_operations = {
  5983. .permission = nfs_permission,
  5984. .getattr = nfs_getattr,
  5985. .setattr = nfs_setattr,
  5986. .getxattr = generic_getxattr,
  5987. .setxattr = generic_setxattr,
  5988. .listxattr = generic_listxattr,
  5989. .removexattr = generic_removexattr,
  5990. };
  5991. const struct nfs_rpc_ops nfs_v4_clientops = {
  5992. .version = 4, /* protocol version */
  5993. .dentry_ops = &nfs4_dentry_operations,
  5994. .dir_inode_ops = &nfs4_dir_inode_operations,
  5995. .file_inode_ops = &nfs4_file_inode_operations,
  5996. .file_ops = &nfs4_file_operations,
  5997. .getroot = nfs4_proc_get_root,
  5998. .submount = nfs4_submount,
  5999. .try_mount = nfs4_try_mount,
  6000. .getattr = nfs4_proc_getattr,
  6001. .setattr = nfs4_proc_setattr,
  6002. .lookup = nfs4_proc_lookup,
  6003. .access = nfs4_proc_access,
  6004. .readlink = nfs4_proc_readlink,
  6005. .create = nfs4_proc_create,
  6006. .remove = nfs4_proc_remove,
  6007. .unlink_setup = nfs4_proc_unlink_setup,
  6008. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  6009. .unlink_done = nfs4_proc_unlink_done,
  6010. .rename = nfs4_proc_rename,
  6011. .rename_setup = nfs4_proc_rename_setup,
  6012. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  6013. .rename_done = nfs4_proc_rename_done,
  6014. .link = nfs4_proc_link,
  6015. .symlink = nfs4_proc_symlink,
  6016. .mkdir = nfs4_proc_mkdir,
  6017. .rmdir = nfs4_proc_remove,
  6018. .readdir = nfs4_proc_readdir,
  6019. .mknod = nfs4_proc_mknod,
  6020. .statfs = nfs4_proc_statfs,
  6021. .fsinfo = nfs4_proc_fsinfo,
  6022. .pathconf = nfs4_proc_pathconf,
  6023. .set_capabilities = nfs4_server_capabilities,
  6024. .decode_dirent = nfs4_decode_dirent,
  6025. .read_setup = nfs4_proc_read_setup,
  6026. .read_pageio_init = pnfs_pageio_init_read,
  6027. .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
  6028. .read_done = nfs4_read_done,
  6029. .write_setup = nfs4_proc_write_setup,
  6030. .write_pageio_init = pnfs_pageio_init_write,
  6031. .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
  6032. .write_done = nfs4_write_done,
  6033. .commit_setup = nfs4_proc_commit_setup,
  6034. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  6035. .commit_done = nfs4_commit_done,
  6036. .lock = nfs4_proc_lock,
  6037. .clear_acl_cache = nfs4_zap_acl_attr,
  6038. .close_context = nfs4_close_context,
  6039. .open_context = nfs4_atomic_open,
  6040. .have_delegation = nfs4_have_delegation,
  6041. .return_delegation = nfs4_inode_return_delegation,
  6042. .alloc_client = nfs4_alloc_client,
  6043. .init_client = nfs4_init_client,
  6044. .free_client = nfs4_free_client,
  6045. .create_server = nfs4_create_server,
  6046. .clone_server = nfs_clone_server,
  6047. };
  6048. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  6049. .prefix = XATTR_NAME_NFSV4_ACL,
  6050. .list = nfs4_xattr_list_nfs4_acl,
  6051. .get = nfs4_xattr_get_nfs4_acl,
  6052. .set = nfs4_xattr_set_nfs4_acl,
  6053. };
  6054. const struct xattr_handler *nfs4_xattr_handlers[] = {
  6055. &nfs4_xattr_nfs4_acl_handler,
  6056. NULL
  6057. };
  6058. /*
  6059. * Local variables:
  6060. * c-basic-offset: 8
  6061. * End:
  6062. */