nfs4proc.c 176 KB

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