nfs4proc.c 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423
  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/slab.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include <linux/nfs.h>
  44. #include <linux/nfs4.h>
  45. #include <linux/nfs_fs.h>
  46. #include <linux/nfs_page.h>
  47. #include <linux/namei.h>
  48. #include <linux/mount.h>
  49. #include <linux/module.h>
  50. #include <linux/sunrpc/bc_xprt.h>
  51. #include "nfs4_fs.h"
  52. #include "delegation.h"
  53. #include "internal.h"
  54. #include "iostat.h"
  55. #include "callback.h"
  56. #define NFSDBG_FACILITY NFSDBG_PROC
  57. #define NFS4_POLL_RETRY_MIN (HZ/10)
  58. #define NFS4_POLL_RETRY_MAX (15*HZ)
  59. #define NFS4_MAX_LOOP_ON_RECOVER (10)
  60. struct nfs4_opendata;
  61. static int _nfs4_proc_open(struct nfs4_opendata *data);
  62. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  63. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  64. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  65. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  66. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  67. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  68. struct nfs_fattr *fattr, struct iattr *sattr,
  69. struct nfs4_state *state);
  70. /* Prevent leaks of NFSv4 errors into userland */
  71. static int nfs4_map_errors(int err)
  72. {
  73. if (err >= -1000)
  74. return err;
  75. switch (err) {
  76. case -NFS4ERR_RESOURCE:
  77. return -EREMOTEIO;
  78. default:
  79. dprintk("%s could not handle NFSv4 error %d\n",
  80. __func__, -err);
  81. break;
  82. }
  83. return -EIO;
  84. }
  85. /*
  86. * This is our standard bitmap for GETATTR requests.
  87. */
  88. const u32 nfs4_fattr_bitmap[2] = {
  89. FATTR4_WORD0_TYPE
  90. | FATTR4_WORD0_CHANGE
  91. | FATTR4_WORD0_SIZE
  92. | FATTR4_WORD0_FSID
  93. | FATTR4_WORD0_FILEID,
  94. FATTR4_WORD1_MODE
  95. | FATTR4_WORD1_NUMLINKS
  96. | FATTR4_WORD1_OWNER
  97. | FATTR4_WORD1_OWNER_GROUP
  98. | FATTR4_WORD1_RAWDEV
  99. | FATTR4_WORD1_SPACE_USED
  100. | FATTR4_WORD1_TIME_ACCESS
  101. | FATTR4_WORD1_TIME_METADATA
  102. | FATTR4_WORD1_TIME_MODIFY
  103. };
  104. const u32 nfs4_statfs_bitmap[2] = {
  105. FATTR4_WORD0_FILES_AVAIL
  106. | FATTR4_WORD0_FILES_FREE
  107. | FATTR4_WORD0_FILES_TOTAL,
  108. FATTR4_WORD1_SPACE_AVAIL
  109. | FATTR4_WORD1_SPACE_FREE
  110. | FATTR4_WORD1_SPACE_TOTAL
  111. };
  112. const u32 nfs4_pathconf_bitmap[2] = {
  113. FATTR4_WORD0_MAXLINK
  114. | FATTR4_WORD0_MAXNAME,
  115. 0
  116. };
  117. const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
  118. | FATTR4_WORD0_MAXREAD
  119. | FATTR4_WORD0_MAXWRITE
  120. | FATTR4_WORD0_LEASE_TIME,
  121. 0
  122. };
  123. const u32 nfs4_fs_locations_bitmap[2] = {
  124. FATTR4_WORD0_TYPE
  125. | FATTR4_WORD0_CHANGE
  126. | FATTR4_WORD0_SIZE
  127. | FATTR4_WORD0_FSID
  128. | FATTR4_WORD0_FILEID
  129. | FATTR4_WORD0_FS_LOCATIONS,
  130. FATTR4_WORD1_MODE
  131. | FATTR4_WORD1_NUMLINKS
  132. | FATTR4_WORD1_OWNER
  133. | FATTR4_WORD1_OWNER_GROUP
  134. | FATTR4_WORD1_RAWDEV
  135. | FATTR4_WORD1_SPACE_USED
  136. | FATTR4_WORD1_TIME_ACCESS
  137. | FATTR4_WORD1_TIME_METADATA
  138. | FATTR4_WORD1_TIME_MODIFY
  139. | FATTR4_WORD1_MOUNTED_ON_FILEID
  140. };
  141. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  142. struct nfs4_readdir_arg *readdir)
  143. {
  144. __be32 *start, *p;
  145. BUG_ON(readdir->count < 80);
  146. if (cookie > 2) {
  147. readdir->cookie = cookie;
  148. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  149. return;
  150. }
  151. readdir->cookie = 0;
  152. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  153. if (cookie == 2)
  154. return;
  155. /*
  156. * NFSv4 servers do not return entries for '.' and '..'
  157. * Therefore, we fake these entries here. We let '.'
  158. * have cookie 0 and '..' have cookie 1. Note that
  159. * when talking to the server, we always send cookie 0
  160. * instead of 1 or 2.
  161. */
  162. start = p = kmap_atomic(*readdir->pages, KM_USER0);
  163. if (cookie == 0) {
  164. *p++ = xdr_one; /* next */
  165. *p++ = xdr_zero; /* cookie, first word */
  166. *p++ = xdr_one; /* cookie, second word */
  167. *p++ = xdr_one; /* entry len */
  168. memcpy(p, ".\0\0\0", 4); /* entry */
  169. p++;
  170. *p++ = xdr_one; /* bitmap length */
  171. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  172. *p++ = htonl(8); /* attribute buffer length */
  173. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  174. }
  175. *p++ = xdr_one; /* next */
  176. *p++ = xdr_zero; /* cookie, first word */
  177. *p++ = xdr_two; /* cookie, second word */
  178. *p++ = xdr_two; /* entry len */
  179. memcpy(p, "..\0\0", 4); /* entry */
  180. p++;
  181. *p++ = xdr_one; /* bitmap length */
  182. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  183. *p++ = htonl(8); /* attribute buffer length */
  184. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  185. readdir->pgbase = (char *)p - (char *)start;
  186. readdir->count -= readdir->pgbase;
  187. kunmap_atomic(start, KM_USER0);
  188. }
  189. static int nfs4_wait_clnt_recover(struct nfs_client *clp)
  190. {
  191. int res;
  192. might_sleep();
  193. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  194. nfs_wait_bit_killable, TASK_KILLABLE);
  195. return res;
  196. }
  197. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  198. {
  199. int res = 0;
  200. might_sleep();
  201. if (*timeout <= 0)
  202. *timeout = NFS4_POLL_RETRY_MIN;
  203. if (*timeout > NFS4_POLL_RETRY_MAX)
  204. *timeout = NFS4_POLL_RETRY_MAX;
  205. schedule_timeout_killable(*timeout);
  206. if (fatal_signal_pending(current))
  207. res = -ERESTARTSYS;
  208. *timeout <<= 1;
  209. return res;
  210. }
  211. /* This is the error handling routine for processes that are allowed
  212. * to sleep.
  213. */
  214. static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  215. {
  216. struct nfs_client *clp = server->nfs_client;
  217. struct nfs4_state *state = exception->state;
  218. int ret = errorcode;
  219. exception->retry = 0;
  220. switch(errorcode) {
  221. case 0:
  222. return 0;
  223. case -NFS4ERR_ADMIN_REVOKED:
  224. case -NFS4ERR_BAD_STATEID:
  225. case -NFS4ERR_OPENMODE:
  226. if (state == NULL)
  227. break;
  228. nfs4_state_mark_reclaim_nograce(clp, state);
  229. goto do_state_recovery;
  230. case -NFS4ERR_STALE_STATEID:
  231. if (state == NULL)
  232. break;
  233. nfs4_state_mark_reclaim_reboot(clp, state);
  234. case -NFS4ERR_STALE_CLIENTID:
  235. case -NFS4ERR_EXPIRED:
  236. goto do_state_recovery;
  237. #if defined(CONFIG_NFS_V4_1)
  238. case -NFS4ERR_BADSESSION:
  239. case -NFS4ERR_BADSLOT:
  240. case -NFS4ERR_BAD_HIGH_SLOT:
  241. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  242. case -NFS4ERR_DEADSESSION:
  243. case -NFS4ERR_SEQ_FALSE_RETRY:
  244. case -NFS4ERR_SEQ_MISORDERED:
  245. dprintk("%s ERROR: %d Reset session\n", __func__,
  246. errorcode);
  247. nfs4_schedule_state_recovery(clp);
  248. exception->retry = 1;
  249. break;
  250. #endif /* defined(CONFIG_NFS_V4_1) */
  251. case -NFS4ERR_FILE_OPEN:
  252. if (exception->timeout > HZ) {
  253. /* We have retried a decent amount, time to
  254. * fail
  255. */
  256. ret = -EBUSY;
  257. break;
  258. }
  259. case -NFS4ERR_GRACE:
  260. case -NFS4ERR_DELAY:
  261. case -EKEYEXPIRED:
  262. ret = nfs4_delay(server->client, &exception->timeout);
  263. if (ret != 0)
  264. break;
  265. case -NFS4ERR_OLD_STATEID:
  266. exception->retry = 1;
  267. }
  268. /* We failed to handle the error */
  269. return nfs4_map_errors(ret);
  270. do_state_recovery:
  271. nfs4_schedule_state_recovery(clp);
  272. ret = nfs4_wait_clnt_recover(clp);
  273. if (ret == 0)
  274. exception->retry = 1;
  275. return ret;
  276. }
  277. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  278. {
  279. struct nfs_client *clp = server->nfs_client;
  280. spin_lock(&clp->cl_lock);
  281. if (time_before(clp->cl_last_renewal,timestamp))
  282. clp->cl_last_renewal = timestamp;
  283. spin_unlock(&clp->cl_lock);
  284. }
  285. #if defined(CONFIG_NFS_V4_1)
  286. /*
  287. * nfs4_free_slot - free a slot and efficiently update slot table.
  288. *
  289. * freeing a slot is trivially done by clearing its respective bit
  290. * in the bitmap.
  291. * If the freed slotid equals highest_used_slotid we want to update it
  292. * so that the server would be able to size down the slot table if needed,
  293. * otherwise we know that the highest_used_slotid is still in use.
  294. * When updating highest_used_slotid there may be "holes" in the bitmap
  295. * so we need to scan down from highest_used_slotid to 0 looking for the now
  296. * highest slotid in use.
  297. * If none found, highest_used_slotid is set to -1.
  298. *
  299. * Must be called while holding tbl->slot_tbl_lock
  300. */
  301. static void
  302. nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
  303. {
  304. int slotid = free_slotid;
  305. /* clear used bit in bitmap */
  306. __clear_bit(slotid, tbl->used_slots);
  307. /* update highest_used_slotid when it is freed */
  308. if (slotid == tbl->highest_used_slotid) {
  309. slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
  310. if (slotid < tbl->max_slots)
  311. tbl->highest_used_slotid = slotid;
  312. else
  313. tbl->highest_used_slotid = -1;
  314. }
  315. dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
  316. free_slotid, tbl->highest_used_slotid);
  317. }
  318. /*
  319. * Signal state manager thread if session is drained
  320. */
  321. static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
  322. {
  323. struct rpc_task *task;
  324. if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
  325. task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
  326. if (task)
  327. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  328. return;
  329. }
  330. if (ses->fc_slot_table.highest_used_slotid != -1)
  331. return;
  332. dprintk("%s COMPLETE: Session Drained\n", __func__);
  333. complete(&ses->complete);
  334. }
  335. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  336. {
  337. struct nfs4_slot_table *tbl;
  338. tbl = &res->sr_session->fc_slot_table;
  339. if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
  340. /* just wake up the next guy waiting since
  341. * we may have not consumed a slot after all */
  342. dprintk("%s: No slot\n", __func__);
  343. return;
  344. }
  345. spin_lock(&tbl->slot_tbl_lock);
  346. nfs4_free_slot(tbl, res->sr_slotid);
  347. nfs41_check_drain_session_complete(res->sr_session);
  348. spin_unlock(&tbl->slot_tbl_lock);
  349. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  350. }
  351. static void nfs41_sequence_done(struct nfs4_sequence_res *res)
  352. {
  353. unsigned long timestamp;
  354. struct nfs4_slot_table *tbl;
  355. struct nfs4_slot *slot;
  356. /*
  357. * sr_status remains 1 if an RPC level error occurred. The server
  358. * may or may not have processed the sequence operation..
  359. * Proceed as if the server received and processed the sequence
  360. * operation.
  361. */
  362. if (res->sr_status == 1)
  363. res->sr_status = NFS_OK;
  364. /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
  365. if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
  366. goto out;
  367. /* Check the SEQUENCE operation status */
  368. if (res->sr_status == 0) {
  369. struct nfs_client *clp = res->sr_session->clp;
  370. tbl = &res->sr_session->fc_slot_table;
  371. slot = tbl->slots + res->sr_slotid;
  372. /* Update the slot's sequence and clientid lease timer */
  373. ++slot->seq_nr;
  374. timestamp = res->sr_renewal_time;
  375. spin_lock(&clp->cl_lock);
  376. if (time_before(clp->cl_last_renewal, timestamp))
  377. clp->cl_last_renewal = timestamp;
  378. spin_unlock(&clp->cl_lock);
  379. /* Check sequence flags */
  380. if (atomic_read(&clp->cl_count) > 1)
  381. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
  382. }
  383. out:
  384. /* The session may be reset by one of the error handlers. */
  385. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  386. nfs41_sequence_free_slot(res);
  387. }
  388. static void nfs4_sequence_done(const struct nfs_server *server,
  389. struct nfs4_sequence_res *res, int rpc_status)
  390. {
  391. if (res->sr_session != NULL)
  392. nfs41_sequence_done(res);
  393. }
  394. /*
  395. * nfs4_find_slot - efficiently look for a free slot
  396. *
  397. * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
  398. * If found, we mark the slot as used, update the highest_used_slotid,
  399. * and respectively set up the sequence operation args.
  400. * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
  401. *
  402. * Note: must be called with under the slot_tbl_lock.
  403. */
  404. static u8
  405. nfs4_find_slot(struct nfs4_slot_table *tbl)
  406. {
  407. int slotid;
  408. u8 ret_id = NFS4_MAX_SLOT_TABLE;
  409. BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
  410. dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
  411. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  412. tbl->max_slots);
  413. slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
  414. if (slotid >= tbl->max_slots)
  415. goto out;
  416. __set_bit(slotid, tbl->used_slots);
  417. if (slotid > tbl->highest_used_slotid)
  418. tbl->highest_used_slotid = slotid;
  419. ret_id = slotid;
  420. out:
  421. dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
  422. __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
  423. return ret_id;
  424. }
  425. static int nfs41_setup_sequence(struct nfs4_session *session,
  426. struct nfs4_sequence_args *args,
  427. struct nfs4_sequence_res *res,
  428. int cache_reply,
  429. struct rpc_task *task)
  430. {
  431. struct nfs4_slot *slot;
  432. struct nfs4_slot_table *tbl;
  433. u8 slotid;
  434. dprintk("--> %s\n", __func__);
  435. /* slot already allocated? */
  436. if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
  437. return 0;
  438. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  439. tbl = &session->fc_slot_table;
  440. spin_lock(&tbl->slot_tbl_lock);
  441. if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
  442. !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
  443. /*
  444. * The state manager will wait until the slot table is empty.
  445. * Schedule the reset thread
  446. */
  447. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  448. spin_unlock(&tbl->slot_tbl_lock);
  449. dprintk("%s Schedule Session Reset\n", __func__);
  450. return -EAGAIN;
  451. }
  452. if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
  453. !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
  454. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  455. spin_unlock(&tbl->slot_tbl_lock);
  456. dprintk("%s enforce FIFO order\n", __func__);
  457. return -EAGAIN;
  458. }
  459. slotid = nfs4_find_slot(tbl);
  460. if (slotid == NFS4_MAX_SLOT_TABLE) {
  461. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  462. spin_unlock(&tbl->slot_tbl_lock);
  463. dprintk("<-- %s: no free slots\n", __func__);
  464. return -EAGAIN;
  465. }
  466. spin_unlock(&tbl->slot_tbl_lock);
  467. rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
  468. slot = tbl->slots + slotid;
  469. args->sa_session = session;
  470. args->sa_slotid = slotid;
  471. args->sa_cache_this = cache_reply;
  472. dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
  473. res->sr_session = session;
  474. res->sr_slotid = slotid;
  475. res->sr_renewal_time = jiffies;
  476. res->sr_status_flags = 0;
  477. /*
  478. * sr_status is only set in decode_sequence, and so will remain
  479. * set to 1 if an rpc level failure occurs.
  480. */
  481. res->sr_status = 1;
  482. return 0;
  483. }
  484. int nfs4_setup_sequence(const struct nfs_server *server,
  485. struct nfs4_sequence_args *args,
  486. struct nfs4_sequence_res *res,
  487. int cache_reply,
  488. struct rpc_task *task)
  489. {
  490. struct nfs4_session *session = nfs4_get_session(server);
  491. int ret = 0;
  492. if (session == NULL) {
  493. args->sa_session = NULL;
  494. res->sr_session = NULL;
  495. goto out;
  496. }
  497. dprintk("--> %s clp %p session %p sr_slotid %d\n",
  498. __func__, session->clp, session, res->sr_slotid);
  499. ret = nfs41_setup_sequence(session, args, res, cache_reply,
  500. task);
  501. out:
  502. dprintk("<-- %s status=%d\n", __func__, ret);
  503. return ret;
  504. }
  505. struct nfs41_call_sync_data {
  506. const struct nfs_server *seq_server;
  507. struct nfs4_sequence_args *seq_args;
  508. struct nfs4_sequence_res *seq_res;
  509. int cache_reply;
  510. };
  511. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  512. {
  513. struct nfs41_call_sync_data *data = calldata;
  514. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  515. if (nfs4_setup_sequence(data->seq_server, data->seq_args,
  516. data->seq_res, data->cache_reply, task))
  517. return;
  518. rpc_call_start(task);
  519. }
  520. static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
  521. {
  522. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  523. nfs41_call_sync_prepare(task, calldata);
  524. }
  525. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  526. {
  527. struct nfs41_call_sync_data *data = calldata;
  528. nfs41_sequence_done(data->seq_res);
  529. }
  530. struct rpc_call_ops nfs41_call_sync_ops = {
  531. .rpc_call_prepare = nfs41_call_sync_prepare,
  532. .rpc_call_done = nfs41_call_sync_done,
  533. };
  534. struct rpc_call_ops nfs41_call_priv_sync_ops = {
  535. .rpc_call_prepare = nfs41_call_priv_sync_prepare,
  536. .rpc_call_done = nfs41_call_sync_done,
  537. };
  538. static int nfs4_call_sync_sequence(struct nfs_server *server,
  539. struct rpc_message *msg,
  540. struct nfs4_sequence_args *args,
  541. struct nfs4_sequence_res *res,
  542. int cache_reply,
  543. int privileged)
  544. {
  545. int ret;
  546. struct rpc_task *task;
  547. struct nfs41_call_sync_data data = {
  548. .seq_server = server,
  549. .seq_args = args,
  550. .seq_res = res,
  551. .cache_reply = cache_reply,
  552. };
  553. struct rpc_task_setup task_setup = {
  554. .rpc_client = server->client,
  555. .rpc_message = msg,
  556. .callback_ops = &nfs41_call_sync_ops,
  557. .callback_data = &data
  558. };
  559. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  560. if (privileged)
  561. task_setup.callback_ops = &nfs41_call_priv_sync_ops;
  562. task = rpc_run_task(&task_setup);
  563. if (IS_ERR(task))
  564. ret = PTR_ERR(task);
  565. else {
  566. ret = task->tk_status;
  567. rpc_put_task(task);
  568. }
  569. return ret;
  570. }
  571. int _nfs4_call_sync_session(struct nfs_server *server,
  572. struct rpc_message *msg,
  573. struct nfs4_sequence_args *args,
  574. struct nfs4_sequence_res *res,
  575. int cache_reply)
  576. {
  577. return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
  578. }
  579. #else
  580. static void nfs4_sequence_done(const struct nfs_server *server,
  581. struct nfs4_sequence_res *res, int rpc_status)
  582. {
  583. }
  584. #endif /* CONFIG_NFS_V4_1 */
  585. int _nfs4_call_sync(struct nfs_server *server,
  586. struct rpc_message *msg,
  587. struct nfs4_sequence_args *args,
  588. struct nfs4_sequence_res *res,
  589. int cache_reply)
  590. {
  591. args->sa_session = res->sr_session = NULL;
  592. return rpc_call_sync(server->client, msg, 0);
  593. }
  594. #define nfs4_call_sync(server, msg, args, res, cache_reply) \
  595. (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
  596. &(res)->seq_res, (cache_reply))
  597. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  598. {
  599. struct nfs_inode *nfsi = NFS_I(dir);
  600. spin_lock(&dir->i_lock);
  601. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
  602. if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
  603. nfs_force_lookup_revalidate(dir);
  604. nfsi->change_attr = cinfo->after;
  605. spin_unlock(&dir->i_lock);
  606. }
  607. struct nfs4_opendata {
  608. struct kref kref;
  609. struct nfs_openargs o_arg;
  610. struct nfs_openres o_res;
  611. struct nfs_open_confirmargs c_arg;
  612. struct nfs_open_confirmres c_res;
  613. struct nfs_fattr f_attr;
  614. struct nfs_fattr dir_attr;
  615. struct path path;
  616. struct dentry *dir;
  617. struct nfs4_state_owner *owner;
  618. struct nfs4_state *state;
  619. struct iattr attrs;
  620. unsigned long timestamp;
  621. unsigned int rpc_done : 1;
  622. int rpc_status;
  623. int cancelled;
  624. };
  625. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  626. {
  627. p->o_res.f_attr = &p->f_attr;
  628. p->o_res.dir_attr = &p->dir_attr;
  629. p->o_res.seqid = p->o_arg.seqid;
  630. p->c_res.seqid = p->c_arg.seqid;
  631. p->o_res.server = p->o_arg.server;
  632. nfs_fattr_init(&p->f_attr);
  633. nfs_fattr_init(&p->dir_attr);
  634. p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  635. }
  636. static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
  637. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  638. const struct iattr *attrs,
  639. gfp_t gfp_mask)
  640. {
  641. struct dentry *parent = dget_parent(path->dentry);
  642. struct inode *dir = parent->d_inode;
  643. struct nfs_server *server = NFS_SERVER(dir);
  644. struct nfs4_opendata *p;
  645. p = kzalloc(sizeof(*p), gfp_mask);
  646. if (p == NULL)
  647. goto err;
  648. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
  649. if (p->o_arg.seqid == NULL)
  650. goto err_free;
  651. path_get(path);
  652. p->path = *path;
  653. p->dir = parent;
  654. p->owner = sp;
  655. atomic_inc(&sp->so_count);
  656. p->o_arg.fh = NFS_FH(dir);
  657. p->o_arg.open_flags = flags;
  658. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  659. p->o_arg.clientid = server->nfs_client->cl_clientid;
  660. p->o_arg.id = sp->so_owner_id.id;
  661. p->o_arg.name = &p->path.dentry->d_name;
  662. p->o_arg.server = server;
  663. p->o_arg.bitmask = server->attr_bitmask;
  664. p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
  665. if (flags & O_CREAT) {
  666. u32 *s;
  667. p->o_arg.u.attrs = &p->attrs;
  668. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  669. s = (u32 *) p->o_arg.u.verifier.data;
  670. s[0] = jiffies;
  671. s[1] = current->pid;
  672. }
  673. p->c_arg.fh = &p->o_res.fh;
  674. p->c_arg.stateid = &p->o_res.stateid;
  675. p->c_arg.seqid = p->o_arg.seqid;
  676. nfs4_init_opendata_res(p);
  677. kref_init(&p->kref);
  678. return p;
  679. err_free:
  680. kfree(p);
  681. err:
  682. dput(parent);
  683. return NULL;
  684. }
  685. static void nfs4_opendata_free(struct kref *kref)
  686. {
  687. struct nfs4_opendata *p = container_of(kref,
  688. struct nfs4_opendata, kref);
  689. nfs_free_seqid(p->o_arg.seqid);
  690. if (p->state != NULL)
  691. nfs4_put_open_state(p->state);
  692. nfs4_put_state_owner(p->owner);
  693. dput(p->dir);
  694. path_put(&p->path);
  695. kfree(p);
  696. }
  697. static void nfs4_opendata_put(struct nfs4_opendata *p)
  698. {
  699. if (p != NULL)
  700. kref_put(&p->kref, nfs4_opendata_free);
  701. }
  702. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  703. {
  704. int ret;
  705. ret = rpc_wait_for_completion_task(task);
  706. return ret;
  707. }
  708. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  709. {
  710. int ret = 0;
  711. if (open_mode & O_EXCL)
  712. goto out;
  713. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  714. case FMODE_READ:
  715. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  716. && state->n_rdonly != 0;
  717. break;
  718. case FMODE_WRITE:
  719. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  720. && state->n_wronly != 0;
  721. break;
  722. case FMODE_READ|FMODE_WRITE:
  723. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  724. && state->n_rdwr != 0;
  725. }
  726. out:
  727. return ret;
  728. }
  729. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  730. {
  731. if ((delegation->type & fmode) != fmode)
  732. return 0;
  733. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  734. return 0;
  735. nfs_mark_delegation_referenced(delegation);
  736. return 1;
  737. }
  738. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  739. {
  740. switch (fmode) {
  741. case FMODE_WRITE:
  742. state->n_wronly++;
  743. break;
  744. case FMODE_READ:
  745. state->n_rdonly++;
  746. break;
  747. case FMODE_READ|FMODE_WRITE:
  748. state->n_rdwr++;
  749. }
  750. nfs4_state_set_mode_locked(state, state->state | fmode);
  751. }
  752. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  753. {
  754. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  755. memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
  756. memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
  757. switch (fmode) {
  758. case FMODE_READ:
  759. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  760. break;
  761. case FMODE_WRITE:
  762. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  763. break;
  764. case FMODE_READ|FMODE_WRITE:
  765. set_bit(NFS_O_RDWR_STATE, &state->flags);
  766. }
  767. }
  768. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  769. {
  770. write_seqlock(&state->seqlock);
  771. nfs_set_open_stateid_locked(state, stateid, fmode);
  772. write_sequnlock(&state->seqlock);
  773. }
  774. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  775. {
  776. /*
  777. * Protect the call to nfs4_state_set_mode_locked and
  778. * serialise the stateid update
  779. */
  780. write_seqlock(&state->seqlock);
  781. if (deleg_stateid != NULL) {
  782. memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
  783. set_bit(NFS_DELEGATED_STATE, &state->flags);
  784. }
  785. if (open_stateid != NULL)
  786. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  787. write_sequnlock(&state->seqlock);
  788. spin_lock(&state->owner->so_lock);
  789. update_open_stateflags(state, fmode);
  790. spin_unlock(&state->owner->so_lock);
  791. }
  792. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  793. {
  794. struct nfs_inode *nfsi = NFS_I(state->inode);
  795. struct nfs_delegation *deleg_cur;
  796. int ret = 0;
  797. fmode &= (FMODE_READ|FMODE_WRITE);
  798. rcu_read_lock();
  799. deleg_cur = rcu_dereference(nfsi->delegation);
  800. if (deleg_cur == NULL)
  801. goto no_delegation;
  802. spin_lock(&deleg_cur->lock);
  803. if (nfsi->delegation != deleg_cur ||
  804. (deleg_cur->type & fmode) != fmode)
  805. goto no_delegation_unlock;
  806. if (delegation == NULL)
  807. delegation = &deleg_cur->stateid;
  808. else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
  809. goto no_delegation_unlock;
  810. nfs_mark_delegation_referenced(deleg_cur);
  811. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  812. ret = 1;
  813. no_delegation_unlock:
  814. spin_unlock(&deleg_cur->lock);
  815. no_delegation:
  816. rcu_read_unlock();
  817. if (!ret && open_stateid != NULL) {
  818. __update_open_stateid(state, open_stateid, NULL, fmode);
  819. ret = 1;
  820. }
  821. return ret;
  822. }
  823. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  824. {
  825. struct nfs_delegation *delegation;
  826. rcu_read_lock();
  827. delegation = rcu_dereference(NFS_I(inode)->delegation);
  828. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  829. rcu_read_unlock();
  830. return;
  831. }
  832. rcu_read_unlock();
  833. nfs_inode_return_delegation(inode);
  834. }
  835. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  836. {
  837. struct nfs4_state *state = opendata->state;
  838. struct nfs_inode *nfsi = NFS_I(state->inode);
  839. struct nfs_delegation *delegation;
  840. int open_mode = opendata->o_arg.open_flags & O_EXCL;
  841. fmode_t fmode = opendata->o_arg.fmode;
  842. nfs4_stateid stateid;
  843. int ret = -EAGAIN;
  844. for (;;) {
  845. if (can_open_cached(state, fmode, open_mode)) {
  846. spin_lock(&state->owner->so_lock);
  847. if (can_open_cached(state, fmode, open_mode)) {
  848. update_open_stateflags(state, fmode);
  849. spin_unlock(&state->owner->so_lock);
  850. goto out_return_state;
  851. }
  852. spin_unlock(&state->owner->so_lock);
  853. }
  854. rcu_read_lock();
  855. delegation = rcu_dereference(nfsi->delegation);
  856. if (delegation == NULL ||
  857. !can_open_delegated(delegation, fmode)) {
  858. rcu_read_unlock();
  859. break;
  860. }
  861. /* Save the delegation */
  862. memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
  863. rcu_read_unlock();
  864. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  865. if (ret != 0)
  866. goto out;
  867. ret = -EAGAIN;
  868. /* Try to update the stateid using the delegation */
  869. if (update_open_stateid(state, NULL, &stateid, fmode))
  870. goto out_return_state;
  871. }
  872. out:
  873. return ERR_PTR(ret);
  874. out_return_state:
  875. atomic_inc(&state->count);
  876. return state;
  877. }
  878. static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  879. {
  880. struct inode *inode;
  881. struct nfs4_state *state = NULL;
  882. struct nfs_delegation *delegation;
  883. int ret;
  884. if (!data->rpc_done) {
  885. state = nfs4_try_open_cached(data);
  886. goto out;
  887. }
  888. ret = -EAGAIN;
  889. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  890. goto err;
  891. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  892. ret = PTR_ERR(inode);
  893. if (IS_ERR(inode))
  894. goto err;
  895. ret = -ENOMEM;
  896. state = nfs4_get_open_state(inode, data->owner);
  897. if (state == NULL)
  898. goto err_put_inode;
  899. if (data->o_res.delegation_type != 0) {
  900. int delegation_flags = 0;
  901. rcu_read_lock();
  902. delegation = rcu_dereference(NFS_I(inode)->delegation);
  903. if (delegation)
  904. delegation_flags = delegation->flags;
  905. rcu_read_unlock();
  906. if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  907. nfs_inode_set_delegation(state->inode,
  908. data->owner->so_cred,
  909. &data->o_res);
  910. else
  911. nfs_inode_reclaim_delegation(state->inode,
  912. data->owner->so_cred,
  913. &data->o_res);
  914. }
  915. update_open_stateid(state, &data->o_res.stateid, NULL,
  916. data->o_arg.fmode);
  917. iput(inode);
  918. out:
  919. return state;
  920. err_put_inode:
  921. iput(inode);
  922. err:
  923. return ERR_PTR(ret);
  924. }
  925. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  926. {
  927. struct nfs_inode *nfsi = NFS_I(state->inode);
  928. struct nfs_open_context *ctx;
  929. spin_lock(&state->inode->i_lock);
  930. list_for_each_entry(ctx, &nfsi->open_files, list) {
  931. if (ctx->state != state)
  932. continue;
  933. get_nfs_open_context(ctx);
  934. spin_unlock(&state->inode->i_lock);
  935. return ctx;
  936. }
  937. spin_unlock(&state->inode->i_lock);
  938. return ERR_PTR(-ENOENT);
  939. }
  940. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
  941. {
  942. struct nfs4_opendata *opendata;
  943. opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
  944. if (opendata == NULL)
  945. return ERR_PTR(-ENOMEM);
  946. opendata->state = state;
  947. atomic_inc(&state->count);
  948. return opendata;
  949. }
  950. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  951. {
  952. struct nfs4_state *newstate;
  953. int ret;
  954. opendata->o_arg.open_flags = 0;
  955. opendata->o_arg.fmode = fmode;
  956. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  957. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  958. nfs4_init_opendata_res(opendata);
  959. ret = _nfs4_recover_proc_open(opendata);
  960. if (ret != 0)
  961. return ret;
  962. newstate = nfs4_opendata_to_nfs4_state(opendata);
  963. if (IS_ERR(newstate))
  964. return PTR_ERR(newstate);
  965. nfs4_close_state(&opendata->path, newstate, fmode);
  966. *res = newstate;
  967. return 0;
  968. }
  969. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  970. {
  971. struct nfs4_state *newstate;
  972. int ret;
  973. /* memory barrier prior to reading state->n_* */
  974. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  975. smp_rmb();
  976. if (state->n_rdwr != 0) {
  977. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  978. if (ret != 0)
  979. return ret;
  980. if (newstate != state)
  981. return -ESTALE;
  982. }
  983. if (state->n_wronly != 0) {
  984. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  985. if (ret != 0)
  986. return ret;
  987. if (newstate != state)
  988. return -ESTALE;
  989. }
  990. if (state->n_rdonly != 0) {
  991. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  992. if (ret != 0)
  993. return ret;
  994. if (newstate != state)
  995. return -ESTALE;
  996. }
  997. /*
  998. * We may have performed cached opens for all three recoveries.
  999. * Check if we need to update the current stateid.
  1000. */
  1001. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1002. memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
  1003. write_seqlock(&state->seqlock);
  1004. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1005. memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
  1006. write_sequnlock(&state->seqlock);
  1007. }
  1008. return 0;
  1009. }
  1010. /*
  1011. * OPEN_RECLAIM:
  1012. * reclaim state on the server after a reboot.
  1013. */
  1014. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1015. {
  1016. struct nfs_delegation *delegation;
  1017. struct nfs4_opendata *opendata;
  1018. fmode_t delegation_type = 0;
  1019. int status;
  1020. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1021. if (IS_ERR(opendata))
  1022. return PTR_ERR(opendata);
  1023. opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
  1024. opendata->o_arg.fh = NFS_FH(state->inode);
  1025. rcu_read_lock();
  1026. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1027. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1028. delegation_type = delegation->type;
  1029. rcu_read_unlock();
  1030. opendata->o_arg.u.delegation_type = delegation_type;
  1031. status = nfs4_open_recover(opendata, state);
  1032. nfs4_opendata_put(opendata);
  1033. return status;
  1034. }
  1035. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1036. {
  1037. struct nfs_server *server = NFS_SERVER(state->inode);
  1038. struct nfs4_exception exception = { };
  1039. int err;
  1040. do {
  1041. err = _nfs4_do_open_reclaim(ctx, state);
  1042. if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
  1043. break;
  1044. nfs4_handle_exception(server, err, &exception);
  1045. } while (exception.retry);
  1046. return err;
  1047. }
  1048. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1049. {
  1050. struct nfs_open_context *ctx;
  1051. int ret;
  1052. ctx = nfs4_state_find_open_context(state);
  1053. if (IS_ERR(ctx))
  1054. return PTR_ERR(ctx);
  1055. ret = nfs4_do_open_reclaim(ctx, state);
  1056. put_nfs_open_context(ctx);
  1057. return ret;
  1058. }
  1059. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1060. {
  1061. struct nfs4_opendata *opendata;
  1062. int ret;
  1063. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1064. if (IS_ERR(opendata))
  1065. return PTR_ERR(opendata);
  1066. opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1067. memcpy(opendata->o_arg.u.delegation.data, stateid->data,
  1068. sizeof(opendata->o_arg.u.delegation.data));
  1069. ret = nfs4_open_recover(opendata, state);
  1070. nfs4_opendata_put(opendata);
  1071. return ret;
  1072. }
  1073. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1074. {
  1075. struct nfs4_exception exception = { };
  1076. struct nfs_server *server = NFS_SERVER(state->inode);
  1077. int err;
  1078. do {
  1079. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  1080. switch (err) {
  1081. case 0:
  1082. case -ENOENT:
  1083. case -ESTALE:
  1084. goto out;
  1085. case -NFS4ERR_BADSESSION:
  1086. case -NFS4ERR_BADSLOT:
  1087. case -NFS4ERR_BAD_HIGH_SLOT:
  1088. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1089. case -NFS4ERR_DEADSESSION:
  1090. nfs4_schedule_state_recovery(
  1091. server->nfs_client);
  1092. goto out;
  1093. case -NFS4ERR_STALE_CLIENTID:
  1094. case -NFS4ERR_STALE_STATEID:
  1095. case -NFS4ERR_EXPIRED:
  1096. /* Don't recall a delegation if it was lost */
  1097. nfs4_schedule_state_recovery(server->nfs_client);
  1098. goto out;
  1099. case -ERESTARTSYS:
  1100. /*
  1101. * The show must go on: exit, but mark the
  1102. * stateid as needing recovery.
  1103. */
  1104. case -NFS4ERR_ADMIN_REVOKED:
  1105. case -NFS4ERR_BAD_STATEID:
  1106. nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
  1107. case -ENOMEM:
  1108. err = 0;
  1109. goto out;
  1110. }
  1111. err = nfs4_handle_exception(server, err, &exception);
  1112. } while (exception.retry);
  1113. out:
  1114. return err;
  1115. }
  1116. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1117. {
  1118. struct nfs4_opendata *data = calldata;
  1119. data->rpc_status = task->tk_status;
  1120. if (RPC_ASSASSINATED(task))
  1121. return;
  1122. if (data->rpc_status == 0) {
  1123. memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
  1124. sizeof(data->o_res.stateid.data));
  1125. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1126. renew_lease(data->o_res.server, data->timestamp);
  1127. data->rpc_done = 1;
  1128. }
  1129. }
  1130. static void nfs4_open_confirm_release(void *calldata)
  1131. {
  1132. struct nfs4_opendata *data = calldata;
  1133. struct nfs4_state *state = NULL;
  1134. /* If this request hasn't been cancelled, do nothing */
  1135. if (data->cancelled == 0)
  1136. goto out_free;
  1137. /* In case of error, no cleanup! */
  1138. if (!data->rpc_done)
  1139. goto out_free;
  1140. state = nfs4_opendata_to_nfs4_state(data);
  1141. if (!IS_ERR(state))
  1142. nfs4_close_state(&data->path, state, data->o_arg.fmode);
  1143. out_free:
  1144. nfs4_opendata_put(data);
  1145. }
  1146. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1147. .rpc_call_done = nfs4_open_confirm_done,
  1148. .rpc_release = nfs4_open_confirm_release,
  1149. };
  1150. /*
  1151. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1152. */
  1153. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1154. {
  1155. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1156. struct rpc_task *task;
  1157. struct rpc_message msg = {
  1158. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1159. .rpc_argp = &data->c_arg,
  1160. .rpc_resp = &data->c_res,
  1161. .rpc_cred = data->owner->so_cred,
  1162. };
  1163. struct rpc_task_setup task_setup_data = {
  1164. .rpc_client = server->client,
  1165. .rpc_message = &msg,
  1166. .callback_ops = &nfs4_open_confirm_ops,
  1167. .callback_data = data,
  1168. .workqueue = nfsiod_workqueue,
  1169. .flags = RPC_TASK_ASYNC,
  1170. };
  1171. int status;
  1172. kref_get(&data->kref);
  1173. data->rpc_done = 0;
  1174. data->rpc_status = 0;
  1175. data->timestamp = jiffies;
  1176. task = rpc_run_task(&task_setup_data);
  1177. if (IS_ERR(task))
  1178. return PTR_ERR(task);
  1179. status = nfs4_wait_for_completion_rpc_task(task);
  1180. if (status != 0) {
  1181. data->cancelled = 1;
  1182. smp_wmb();
  1183. } else
  1184. status = data->rpc_status;
  1185. rpc_put_task(task);
  1186. return status;
  1187. }
  1188. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1189. {
  1190. struct nfs4_opendata *data = calldata;
  1191. struct nfs4_state_owner *sp = data->owner;
  1192. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1193. return;
  1194. /*
  1195. * Check if we still need to send an OPEN call, or if we can use
  1196. * a delegation instead.
  1197. */
  1198. if (data->state != NULL) {
  1199. struct nfs_delegation *delegation;
  1200. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1201. goto out_no_action;
  1202. rcu_read_lock();
  1203. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1204. if (delegation != NULL &&
  1205. test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
  1206. rcu_read_unlock();
  1207. goto out_no_action;
  1208. }
  1209. rcu_read_unlock();
  1210. }
  1211. /* Update sequence id. */
  1212. data->o_arg.id = sp->so_owner_id.id;
  1213. data->o_arg.clientid = sp->so_client->cl_clientid;
  1214. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1215. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1216. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1217. }
  1218. data->timestamp = jiffies;
  1219. if (nfs4_setup_sequence(data->o_arg.server,
  1220. &data->o_arg.seq_args,
  1221. &data->o_res.seq_res, 1, task))
  1222. return;
  1223. rpc_call_start(task);
  1224. return;
  1225. out_no_action:
  1226. task->tk_action = NULL;
  1227. }
  1228. static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
  1229. {
  1230. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  1231. nfs4_open_prepare(task, calldata);
  1232. }
  1233. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1234. {
  1235. struct nfs4_opendata *data = calldata;
  1236. data->rpc_status = task->tk_status;
  1237. nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
  1238. task->tk_status);
  1239. if (RPC_ASSASSINATED(task))
  1240. return;
  1241. if (task->tk_status == 0) {
  1242. switch (data->o_res.f_attr->mode & S_IFMT) {
  1243. case S_IFREG:
  1244. break;
  1245. case S_IFLNK:
  1246. data->rpc_status = -ELOOP;
  1247. break;
  1248. case S_IFDIR:
  1249. data->rpc_status = -EISDIR;
  1250. break;
  1251. default:
  1252. data->rpc_status = -ENOTDIR;
  1253. }
  1254. renew_lease(data->o_res.server, data->timestamp);
  1255. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1256. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1257. }
  1258. data->rpc_done = 1;
  1259. }
  1260. static void nfs4_open_release(void *calldata)
  1261. {
  1262. struct nfs4_opendata *data = calldata;
  1263. struct nfs4_state *state = NULL;
  1264. /* If this request hasn't been cancelled, do nothing */
  1265. if (data->cancelled == 0)
  1266. goto out_free;
  1267. /* In case of error, no cleanup! */
  1268. if (data->rpc_status != 0 || !data->rpc_done)
  1269. goto out_free;
  1270. /* In case we need an open_confirm, no cleanup! */
  1271. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1272. goto out_free;
  1273. state = nfs4_opendata_to_nfs4_state(data);
  1274. if (!IS_ERR(state))
  1275. nfs4_close_state(&data->path, state, data->o_arg.fmode);
  1276. out_free:
  1277. nfs4_opendata_put(data);
  1278. }
  1279. static const struct rpc_call_ops nfs4_open_ops = {
  1280. .rpc_call_prepare = nfs4_open_prepare,
  1281. .rpc_call_done = nfs4_open_done,
  1282. .rpc_release = nfs4_open_release,
  1283. };
  1284. static const struct rpc_call_ops nfs4_recover_open_ops = {
  1285. .rpc_call_prepare = nfs4_recover_open_prepare,
  1286. .rpc_call_done = nfs4_open_done,
  1287. .rpc_release = nfs4_open_release,
  1288. };
  1289. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  1290. {
  1291. struct inode *dir = data->dir->d_inode;
  1292. struct nfs_server *server = NFS_SERVER(dir);
  1293. struct nfs_openargs *o_arg = &data->o_arg;
  1294. struct nfs_openres *o_res = &data->o_res;
  1295. struct rpc_task *task;
  1296. struct rpc_message msg = {
  1297. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1298. .rpc_argp = o_arg,
  1299. .rpc_resp = o_res,
  1300. .rpc_cred = data->owner->so_cred,
  1301. };
  1302. struct rpc_task_setup task_setup_data = {
  1303. .rpc_client = server->client,
  1304. .rpc_message = &msg,
  1305. .callback_ops = &nfs4_open_ops,
  1306. .callback_data = data,
  1307. .workqueue = nfsiod_workqueue,
  1308. .flags = RPC_TASK_ASYNC,
  1309. };
  1310. int status;
  1311. kref_get(&data->kref);
  1312. data->rpc_done = 0;
  1313. data->rpc_status = 0;
  1314. data->cancelled = 0;
  1315. if (isrecover)
  1316. task_setup_data.callback_ops = &nfs4_recover_open_ops;
  1317. task = rpc_run_task(&task_setup_data);
  1318. if (IS_ERR(task))
  1319. return PTR_ERR(task);
  1320. status = nfs4_wait_for_completion_rpc_task(task);
  1321. if (status != 0) {
  1322. data->cancelled = 1;
  1323. smp_wmb();
  1324. } else
  1325. status = data->rpc_status;
  1326. rpc_put_task(task);
  1327. return status;
  1328. }
  1329. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  1330. {
  1331. struct inode *dir = data->dir->d_inode;
  1332. struct nfs_openres *o_res = &data->o_res;
  1333. int status;
  1334. status = nfs4_run_open_task(data, 1);
  1335. if (status != 0 || !data->rpc_done)
  1336. return status;
  1337. nfs_refresh_inode(dir, o_res->dir_attr);
  1338. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1339. status = _nfs4_proc_open_confirm(data);
  1340. if (status != 0)
  1341. return status;
  1342. }
  1343. return status;
  1344. }
  1345. /*
  1346. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1347. */
  1348. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1349. {
  1350. struct inode *dir = data->dir->d_inode;
  1351. struct nfs_server *server = NFS_SERVER(dir);
  1352. struct nfs_openargs *o_arg = &data->o_arg;
  1353. struct nfs_openres *o_res = &data->o_res;
  1354. int status;
  1355. status = nfs4_run_open_task(data, 0);
  1356. if (status != 0 || !data->rpc_done)
  1357. return status;
  1358. if (o_arg->open_flags & O_CREAT) {
  1359. update_changeattr(dir, &o_res->cinfo);
  1360. nfs_post_op_update_inode(dir, o_res->dir_attr);
  1361. } else
  1362. nfs_refresh_inode(dir, o_res->dir_attr);
  1363. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  1364. server->caps &= ~NFS_CAP_POSIX_LOCK;
  1365. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1366. status = _nfs4_proc_open_confirm(data);
  1367. if (status != 0)
  1368. return status;
  1369. }
  1370. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1371. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1372. return 0;
  1373. }
  1374. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1375. {
  1376. struct nfs_client *clp = server->nfs_client;
  1377. unsigned int loop;
  1378. int ret;
  1379. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1380. ret = nfs4_wait_clnt_recover(clp);
  1381. if (ret != 0)
  1382. break;
  1383. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1384. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1385. break;
  1386. nfs4_schedule_state_recovery(clp);
  1387. ret = -EIO;
  1388. }
  1389. return ret;
  1390. }
  1391. /*
  1392. * OPEN_EXPIRED:
  1393. * reclaim state on the server after a network partition.
  1394. * Assumes caller holds the appropriate lock
  1395. */
  1396. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1397. {
  1398. struct nfs4_opendata *opendata;
  1399. int ret;
  1400. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1401. if (IS_ERR(opendata))
  1402. return PTR_ERR(opendata);
  1403. ret = nfs4_open_recover(opendata, state);
  1404. if (ret == -ESTALE)
  1405. d_drop(ctx->path.dentry);
  1406. nfs4_opendata_put(opendata);
  1407. return ret;
  1408. }
  1409. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1410. {
  1411. struct nfs_server *server = NFS_SERVER(state->inode);
  1412. struct nfs4_exception exception = { };
  1413. int err;
  1414. do {
  1415. err = _nfs4_open_expired(ctx, state);
  1416. switch (err) {
  1417. default:
  1418. goto out;
  1419. case -NFS4ERR_GRACE:
  1420. case -NFS4ERR_DELAY:
  1421. case -EKEYEXPIRED:
  1422. nfs4_handle_exception(server, err, &exception);
  1423. err = 0;
  1424. }
  1425. } while (exception.retry);
  1426. out:
  1427. return err;
  1428. }
  1429. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1430. {
  1431. struct nfs_open_context *ctx;
  1432. int ret;
  1433. ctx = nfs4_state_find_open_context(state);
  1434. if (IS_ERR(ctx))
  1435. return PTR_ERR(ctx);
  1436. ret = nfs4_do_open_expired(ctx, state);
  1437. put_nfs_open_context(ctx);
  1438. return ret;
  1439. }
  1440. /*
  1441. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1442. * fields corresponding to attributes that were used to store the verifier.
  1443. * Make sure we clobber those fields in the later setattr call
  1444. */
  1445. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1446. {
  1447. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1448. !(sattr->ia_valid & ATTR_ATIME_SET))
  1449. sattr->ia_valid |= ATTR_ATIME;
  1450. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1451. !(sattr->ia_valid & ATTR_MTIME_SET))
  1452. sattr->ia_valid |= ATTR_MTIME;
  1453. }
  1454. /*
  1455. * Returns a referenced nfs4_state
  1456. */
  1457. static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
  1458. {
  1459. struct nfs4_state_owner *sp;
  1460. struct nfs4_state *state = NULL;
  1461. struct nfs_server *server = NFS_SERVER(dir);
  1462. struct nfs4_opendata *opendata;
  1463. int status;
  1464. /* Protect against reboot recovery conflicts */
  1465. status = -ENOMEM;
  1466. if (!(sp = nfs4_get_state_owner(server, cred))) {
  1467. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1468. goto out_err;
  1469. }
  1470. status = nfs4_recover_expired_lease(server);
  1471. if (status != 0)
  1472. goto err_put_state_owner;
  1473. if (path->dentry->d_inode != NULL)
  1474. nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
  1475. status = -ENOMEM;
  1476. opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
  1477. if (opendata == NULL)
  1478. goto err_put_state_owner;
  1479. if (path->dentry->d_inode != NULL)
  1480. opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
  1481. status = _nfs4_proc_open(opendata);
  1482. if (status != 0)
  1483. goto err_opendata_put;
  1484. state = nfs4_opendata_to_nfs4_state(opendata);
  1485. status = PTR_ERR(state);
  1486. if (IS_ERR(state))
  1487. goto err_opendata_put;
  1488. if (server->caps & NFS_CAP_POSIX_LOCK)
  1489. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  1490. if (opendata->o_arg.open_flags & O_EXCL) {
  1491. nfs4_exclusive_attrset(opendata, sattr);
  1492. nfs_fattr_init(opendata->o_res.f_attr);
  1493. status = nfs4_do_setattr(state->inode, cred,
  1494. opendata->o_res.f_attr, sattr,
  1495. state);
  1496. if (status == 0)
  1497. nfs_setattr_update_inode(state->inode, sattr);
  1498. nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
  1499. }
  1500. nfs4_opendata_put(opendata);
  1501. nfs4_put_state_owner(sp);
  1502. *res = state;
  1503. return 0;
  1504. err_opendata_put:
  1505. nfs4_opendata_put(opendata);
  1506. err_put_state_owner:
  1507. nfs4_put_state_owner(sp);
  1508. out_err:
  1509. *res = NULL;
  1510. return status;
  1511. }
  1512. static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
  1513. {
  1514. struct nfs4_exception exception = { };
  1515. struct nfs4_state *res;
  1516. int status;
  1517. do {
  1518. status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
  1519. if (status == 0)
  1520. break;
  1521. /* NOTE: BAD_SEQID means the server and client disagree about the
  1522. * book-keeping w.r.t. state-changing operations
  1523. * (OPEN/CLOSE/LOCK/LOCKU...)
  1524. * It is actually a sign of a bug on the client or on the server.
  1525. *
  1526. * If we receive a BAD_SEQID error in the particular case of
  1527. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1528. * have unhashed the old state_owner for us, and that we can
  1529. * therefore safely retry using a new one. We should still warn
  1530. * the user though...
  1531. */
  1532. if (status == -NFS4ERR_BAD_SEQID) {
  1533. printk(KERN_WARNING "NFS: v4 server %s "
  1534. " returned a bad sequence-id error!\n",
  1535. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1536. exception.retry = 1;
  1537. continue;
  1538. }
  1539. /*
  1540. * BAD_STATEID on OPEN means that the server cancelled our
  1541. * state before it received the OPEN_CONFIRM.
  1542. * Recover by retrying the request as per the discussion
  1543. * on Page 181 of RFC3530.
  1544. */
  1545. if (status == -NFS4ERR_BAD_STATEID) {
  1546. exception.retry = 1;
  1547. continue;
  1548. }
  1549. if (status == -EAGAIN) {
  1550. /* We must have found a delegation */
  1551. exception.retry = 1;
  1552. continue;
  1553. }
  1554. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  1555. status, &exception));
  1556. } while (exception.retry);
  1557. return res;
  1558. }
  1559. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1560. struct nfs_fattr *fattr, struct iattr *sattr,
  1561. struct nfs4_state *state)
  1562. {
  1563. struct nfs_server *server = NFS_SERVER(inode);
  1564. struct nfs_setattrargs arg = {
  1565. .fh = NFS_FH(inode),
  1566. .iap = sattr,
  1567. .server = server,
  1568. .bitmask = server->attr_bitmask,
  1569. };
  1570. struct nfs_setattrres res = {
  1571. .fattr = fattr,
  1572. .server = server,
  1573. };
  1574. struct rpc_message msg = {
  1575. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1576. .rpc_argp = &arg,
  1577. .rpc_resp = &res,
  1578. .rpc_cred = cred,
  1579. };
  1580. unsigned long timestamp = jiffies;
  1581. int status;
  1582. nfs_fattr_init(fattr);
  1583. if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
  1584. /* Use that stateid */
  1585. } else if (state != NULL) {
  1586. nfs4_copy_stateid(&arg.stateid, state, current->files);
  1587. } else
  1588. memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
  1589. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  1590. if (status == 0 && state != NULL)
  1591. renew_lease(server, timestamp);
  1592. return status;
  1593. }
  1594. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1595. struct nfs_fattr *fattr, struct iattr *sattr,
  1596. struct nfs4_state *state)
  1597. {
  1598. struct nfs_server *server = NFS_SERVER(inode);
  1599. struct nfs4_exception exception = { };
  1600. int err;
  1601. do {
  1602. err = nfs4_handle_exception(server,
  1603. _nfs4_do_setattr(inode, cred, fattr, sattr, state),
  1604. &exception);
  1605. } while (exception.retry);
  1606. return err;
  1607. }
  1608. struct nfs4_closedata {
  1609. struct path path;
  1610. struct inode *inode;
  1611. struct nfs4_state *state;
  1612. struct nfs_closeargs arg;
  1613. struct nfs_closeres res;
  1614. struct nfs_fattr fattr;
  1615. unsigned long timestamp;
  1616. };
  1617. static void nfs4_free_closedata(void *data)
  1618. {
  1619. struct nfs4_closedata *calldata = data;
  1620. struct nfs4_state_owner *sp = calldata->state->owner;
  1621. nfs4_put_open_state(calldata->state);
  1622. nfs_free_seqid(calldata->arg.seqid);
  1623. nfs4_put_state_owner(sp);
  1624. path_put(&calldata->path);
  1625. kfree(calldata);
  1626. }
  1627. static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
  1628. fmode_t fmode)
  1629. {
  1630. spin_lock(&state->owner->so_lock);
  1631. if (!(fmode & FMODE_READ))
  1632. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1633. if (!(fmode & FMODE_WRITE))
  1634. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1635. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1636. spin_unlock(&state->owner->so_lock);
  1637. }
  1638. static void nfs4_close_done(struct rpc_task *task, void *data)
  1639. {
  1640. struct nfs4_closedata *calldata = data;
  1641. struct nfs4_state *state = calldata->state;
  1642. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1643. nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
  1644. if (RPC_ASSASSINATED(task))
  1645. return;
  1646. /* hmm. we are done with the inode, and in the process of freeing
  1647. * the state_owner. we keep this around to process errors
  1648. */
  1649. switch (task->tk_status) {
  1650. case 0:
  1651. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1652. renew_lease(server, calldata->timestamp);
  1653. nfs4_close_clear_stateid_flags(state,
  1654. calldata->arg.fmode);
  1655. break;
  1656. case -NFS4ERR_STALE_STATEID:
  1657. case -NFS4ERR_OLD_STATEID:
  1658. case -NFS4ERR_BAD_STATEID:
  1659. case -NFS4ERR_EXPIRED:
  1660. if (calldata->arg.fmode == 0)
  1661. break;
  1662. default:
  1663. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  1664. rpc_restart_call_prepare(task);
  1665. }
  1666. nfs_release_seqid(calldata->arg.seqid);
  1667. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1668. }
  1669. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1670. {
  1671. struct nfs4_closedata *calldata = data;
  1672. struct nfs4_state *state = calldata->state;
  1673. int call_close = 0;
  1674. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1675. return;
  1676. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1677. calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
  1678. spin_lock(&state->owner->so_lock);
  1679. /* Calculate the change in open mode */
  1680. if (state->n_rdwr == 0) {
  1681. if (state->n_rdonly == 0) {
  1682. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  1683. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  1684. calldata->arg.fmode &= ~FMODE_READ;
  1685. }
  1686. if (state->n_wronly == 0) {
  1687. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  1688. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  1689. calldata->arg.fmode &= ~FMODE_WRITE;
  1690. }
  1691. }
  1692. spin_unlock(&state->owner->so_lock);
  1693. if (!call_close) {
  1694. /* Note: exit _without_ calling nfs4_close_done */
  1695. task->tk_action = NULL;
  1696. return;
  1697. }
  1698. if (calldata->arg.fmode == 0)
  1699. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  1700. nfs_fattr_init(calldata->res.fattr);
  1701. calldata->timestamp = jiffies;
  1702. if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
  1703. &calldata->arg.seq_args, &calldata->res.seq_res,
  1704. 1, task))
  1705. return;
  1706. rpc_call_start(task);
  1707. }
  1708. static const struct rpc_call_ops nfs4_close_ops = {
  1709. .rpc_call_prepare = nfs4_close_prepare,
  1710. .rpc_call_done = nfs4_close_done,
  1711. .rpc_release = nfs4_free_closedata,
  1712. };
  1713. /*
  1714. * It is possible for data to be read/written from a mem-mapped file
  1715. * after the sys_close call (which hits the vfs layer as a flush).
  1716. * This means that we can't safely call nfsv4 close on a file until
  1717. * the inode is cleared. This in turn means that we are not good
  1718. * NFSv4 citizens - we do not indicate to the server to update the file's
  1719. * share state even when we are done with one of the three share
  1720. * stateid's in the inode.
  1721. *
  1722. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1723. */
  1724. int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
  1725. {
  1726. struct nfs_server *server = NFS_SERVER(state->inode);
  1727. struct nfs4_closedata *calldata;
  1728. struct nfs4_state_owner *sp = state->owner;
  1729. struct rpc_task *task;
  1730. struct rpc_message msg = {
  1731. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1732. .rpc_cred = state->owner->so_cred,
  1733. };
  1734. struct rpc_task_setup task_setup_data = {
  1735. .rpc_client = server->client,
  1736. .rpc_message = &msg,
  1737. .callback_ops = &nfs4_close_ops,
  1738. .workqueue = nfsiod_workqueue,
  1739. .flags = RPC_TASK_ASYNC,
  1740. };
  1741. int status = -ENOMEM;
  1742. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  1743. if (calldata == NULL)
  1744. goto out;
  1745. calldata->inode = state->inode;
  1746. calldata->state = state;
  1747. calldata->arg.fh = NFS_FH(state->inode);
  1748. calldata->arg.stateid = &state->open_stateid;
  1749. /* Serialization for the sequence id */
  1750. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
  1751. if (calldata->arg.seqid == NULL)
  1752. goto out_free_calldata;
  1753. calldata->arg.fmode = 0;
  1754. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1755. calldata->res.fattr = &calldata->fattr;
  1756. calldata->res.seqid = calldata->arg.seqid;
  1757. calldata->res.server = server;
  1758. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  1759. path_get(path);
  1760. calldata->path = *path;
  1761. msg.rpc_argp = &calldata->arg,
  1762. msg.rpc_resp = &calldata->res,
  1763. task_setup_data.callback_data = calldata;
  1764. task = rpc_run_task(&task_setup_data);
  1765. if (IS_ERR(task))
  1766. return PTR_ERR(task);
  1767. status = 0;
  1768. if (wait)
  1769. status = rpc_wait_for_completion_task(task);
  1770. rpc_put_task(task);
  1771. return status;
  1772. out_free_calldata:
  1773. kfree(calldata);
  1774. out:
  1775. nfs4_put_open_state(state);
  1776. nfs4_put_state_owner(sp);
  1777. return status;
  1778. }
  1779. static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
  1780. {
  1781. struct file *filp;
  1782. int ret;
  1783. /* If the open_intent is for execute, we have an extra check to make */
  1784. if (fmode & FMODE_EXEC) {
  1785. ret = nfs_may_open(state->inode,
  1786. state->owner->so_cred,
  1787. nd->intent.open.flags);
  1788. if (ret < 0)
  1789. goto out_close;
  1790. }
  1791. filp = lookup_instantiate_filp(nd, path->dentry, NULL);
  1792. if (!IS_ERR(filp)) {
  1793. struct nfs_open_context *ctx;
  1794. ctx = nfs_file_open_context(filp);
  1795. ctx->state = state;
  1796. return 0;
  1797. }
  1798. ret = PTR_ERR(filp);
  1799. out_close:
  1800. nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
  1801. return ret;
  1802. }
  1803. struct dentry *
  1804. nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1805. {
  1806. struct path path = {
  1807. .mnt = nd->path.mnt,
  1808. .dentry = dentry,
  1809. };
  1810. struct dentry *parent;
  1811. struct iattr attr;
  1812. struct rpc_cred *cred;
  1813. struct nfs4_state *state;
  1814. struct dentry *res;
  1815. fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1816. if (nd->flags & LOOKUP_CREATE) {
  1817. attr.ia_mode = nd->intent.open.create_mode;
  1818. attr.ia_valid = ATTR_MODE;
  1819. if (!IS_POSIXACL(dir))
  1820. attr.ia_mode &= ~current_umask();
  1821. } else {
  1822. attr.ia_valid = 0;
  1823. BUG_ON(nd->intent.open.flags & O_CREAT);
  1824. }
  1825. cred = rpc_lookup_cred();
  1826. if (IS_ERR(cred))
  1827. return (struct dentry *)cred;
  1828. parent = dentry->d_parent;
  1829. /* Protect against concurrent sillydeletes */
  1830. nfs_block_sillyrename(parent);
  1831. state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
  1832. put_rpccred(cred);
  1833. if (IS_ERR(state)) {
  1834. if (PTR_ERR(state) == -ENOENT) {
  1835. d_add(dentry, NULL);
  1836. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1837. }
  1838. nfs_unblock_sillyrename(parent);
  1839. return (struct dentry *)state;
  1840. }
  1841. res = d_add_unique(dentry, igrab(state->inode));
  1842. if (res != NULL)
  1843. path.dentry = res;
  1844. nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
  1845. nfs_unblock_sillyrename(parent);
  1846. nfs4_intent_set_file(nd, &path, state, fmode);
  1847. return res;
  1848. }
  1849. int
  1850. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
  1851. {
  1852. struct path path = {
  1853. .mnt = nd->path.mnt,
  1854. .dentry = dentry,
  1855. };
  1856. struct rpc_cred *cred;
  1857. struct nfs4_state *state;
  1858. fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
  1859. cred = rpc_lookup_cred();
  1860. if (IS_ERR(cred))
  1861. return PTR_ERR(cred);
  1862. state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
  1863. put_rpccred(cred);
  1864. if (IS_ERR(state)) {
  1865. switch (PTR_ERR(state)) {
  1866. case -EPERM:
  1867. case -EACCES:
  1868. case -EDQUOT:
  1869. case -ENOSPC:
  1870. case -EROFS:
  1871. return PTR_ERR(state);
  1872. default:
  1873. goto out_drop;
  1874. }
  1875. }
  1876. if (state->inode == dentry->d_inode) {
  1877. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1878. nfs4_intent_set_file(nd, &path, state, fmode);
  1879. return 1;
  1880. }
  1881. nfs4_close_sync(&path, state, fmode);
  1882. out_drop:
  1883. d_drop(dentry);
  1884. return 0;
  1885. }
  1886. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  1887. {
  1888. if (ctx->state == NULL)
  1889. return;
  1890. if (is_sync)
  1891. nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
  1892. else
  1893. nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
  1894. }
  1895. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1896. {
  1897. struct nfs4_server_caps_arg args = {
  1898. .fhandle = fhandle,
  1899. };
  1900. struct nfs4_server_caps_res res = {};
  1901. struct rpc_message msg = {
  1902. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  1903. .rpc_argp = &args,
  1904. .rpc_resp = &res,
  1905. };
  1906. int status;
  1907. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1908. if (status == 0) {
  1909. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  1910. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  1911. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  1912. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  1913. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  1914. NFS_CAP_CTIME|NFS_CAP_MTIME);
  1915. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  1916. server->caps |= NFS_CAP_ACLS;
  1917. if (res.has_links != 0)
  1918. server->caps |= NFS_CAP_HARDLINKS;
  1919. if (res.has_symlinks != 0)
  1920. server->caps |= NFS_CAP_SYMLINKS;
  1921. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  1922. server->caps |= NFS_CAP_FILEID;
  1923. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  1924. server->caps |= NFS_CAP_MODE;
  1925. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  1926. server->caps |= NFS_CAP_NLINK;
  1927. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  1928. server->caps |= NFS_CAP_OWNER;
  1929. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  1930. server->caps |= NFS_CAP_OWNER_GROUP;
  1931. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  1932. server->caps |= NFS_CAP_ATIME;
  1933. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  1934. server->caps |= NFS_CAP_CTIME;
  1935. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  1936. server->caps |= NFS_CAP_MTIME;
  1937. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  1938. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  1939. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  1940. server->acl_bitmask = res.acl_bitmask;
  1941. }
  1942. return status;
  1943. }
  1944. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1945. {
  1946. struct nfs4_exception exception = { };
  1947. int err;
  1948. do {
  1949. err = nfs4_handle_exception(server,
  1950. _nfs4_server_capabilities(server, fhandle),
  1951. &exception);
  1952. } while (exception.retry);
  1953. return err;
  1954. }
  1955. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1956. struct nfs_fsinfo *info)
  1957. {
  1958. struct nfs4_lookup_root_arg args = {
  1959. .bitmask = nfs4_fattr_bitmap,
  1960. };
  1961. struct nfs4_lookup_res res = {
  1962. .server = server,
  1963. .fattr = info->fattr,
  1964. .fh = fhandle,
  1965. };
  1966. struct rpc_message msg = {
  1967. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  1968. .rpc_argp = &args,
  1969. .rpc_resp = &res,
  1970. };
  1971. nfs_fattr_init(info->fattr);
  1972. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1973. }
  1974. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1975. struct nfs_fsinfo *info)
  1976. {
  1977. struct nfs4_exception exception = { };
  1978. int err;
  1979. do {
  1980. err = nfs4_handle_exception(server,
  1981. _nfs4_lookup_root(server, fhandle, info),
  1982. &exception);
  1983. } while (exception.retry);
  1984. return err;
  1985. }
  1986. /*
  1987. * get the file handle for the "/" directory on the server
  1988. */
  1989. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1990. struct nfs_fsinfo *info)
  1991. {
  1992. int status;
  1993. status = nfs4_lookup_root(server, fhandle, info);
  1994. if (status == 0)
  1995. status = nfs4_server_capabilities(server, fhandle);
  1996. if (status == 0)
  1997. status = nfs4_do_fsinfo(server, fhandle, info);
  1998. return nfs4_map_errors(status);
  1999. }
  2000. /*
  2001. * Get locations and (maybe) other attributes of a referral.
  2002. * Note that we'll actually follow the referral later when
  2003. * we detect fsid mismatch in inode revalidation
  2004. */
  2005. static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
  2006. {
  2007. int status = -ENOMEM;
  2008. struct page *page = NULL;
  2009. struct nfs4_fs_locations *locations = NULL;
  2010. page = alloc_page(GFP_KERNEL);
  2011. if (page == NULL)
  2012. goto out;
  2013. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2014. if (locations == NULL)
  2015. goto out;
  2016. status = nfs4_proc_fs_locations(dir, name, locations, page);
  2017. if (status != 0)
  2018. goto out;
  2019. /* Make sure server returned a different fsid for the referral */
  2020. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  2021. dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
  2022. status = -EIO;
  2023. goto out;
  2024. }
  2025. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  2026. fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
  2027. if (!fattr->mode)
  2028. fattr->mode = S_IFDIR;
  2029. memset(fhandle, 0, sizeof(struct nfs_fh));
  2030. out:
  2031. if (page)
  2032. __free_page(page);
  2033. if (locations)
  2034. kfree(locations);
  2035. return status;
  2036. }
  2037. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2038. {
  2039. struct nfs4_getattr_arg args = {
  2040. .fh = fhandle,
  2041. .bitmask = server->attr_bitmask,
  2042. };
  2043. struct nfs4_getattr_res res = {
  2044. .fattr = fattr,
  2045. .server = server,
  2046. };
  2047. struct rpc_message msg = {
  2048. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  2049. .rpc_argp = &args,
  2050. .rpc_resp = &res,
  2051. };
  2052. nfs_fattr_init(fattr);
  2053. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2054. }
  2055. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2056. {
  2057. struct nfs4_exception exception = { };
  2058. int err;
  2059. do {
  2060. err = nfs4_handle_exception(server,
  2061. _nfs4_proc_getattr(server, fhandle, fattr),
  2062. &exception);
  2063. } while (exception.retry);
  2064. return err;
  2065. }
  2066. /*
  2067. * The file is not closed if it is opened due to the a request to change
  2068. * the size of the file. The open call will not be needed once the
  2069. * VFS layer lookup-intents are implemented.
  2070. *
  2071. * Close is called when the inode is destroyed.
  2072. * If we haven't opened the file for O_WRONLY, we
  2073. * need to in the size_change case to obtain a stateid.
  2074. *
  2075. * Got race?
  2076. * Because OPEN is always done by name in nfsv4, it is
  2077. * possible that we opened a different file by the same
  2078. * name. We can recognize this race condition, but we
  2079. * can't do anything about it besides returning an error.
  2080. *
  2081. * This will be fixed with VFS changes (lookup-intent).
  2082. */
  2083. static int
  2084. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  2085. struct iattr *sattr)
  2086. {
  2087. struct inode *inode = dentry->d_inode;
  2088. struct rpc_cred *cred = NULL;
  2089. struct nfs4_state *state = NULL;
  2090. int status;
  2091. nfs_fattr_init(fattr);
  2092. /* Search for an existing open(O_WRITE) file */
  2093. if (sattr->ia_valid & ATTR_FILE) {
  2094. struct nfs_open_context *ctx;
  2095. ctx = nfs_file_open_context(sattr->ia_file);
  2096. if (ctx) {
  2097. cred = ctx->cred;
  2098. state = ctx->state;
  2099. }
  2100. }
  2101. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2102. if (status == 0)
  2103. nfs_setattr_update_inode(inode, sattr);
  2104. return status;
  2105. }
  2106. static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
  2107. const struct qstr *name, struct nfs_fh *fhandle,
  2108. struct nfs_fattr *fattr)
  2109. {
  2110. int status;
  2111. struct nfs4_lookup_arg args = {
  2112. .bitmask = server->attr_bitmask,
  2113. .dir_fh = dirfh,
  2114. .name = name,
  2115. };
  2116. struct nfs4_lookup_res res = {
  2117. .server = server,
  2118. .fattr = fattr,
  2119. .fh = fhandle,
  2120. };
  2121. struct rpc_message msg = {
  2122. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2123. .rpc_argp = &args,
  2124. .rpc_resp = &res,
  2125. };
  2126. nfs_fattr_init(fattr);
  2127. dprintk("NFS call lookupfh %s\n", name->name);
  2128. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2129. dprintk("NFS reply lookupfh: %d\n", status);
  2130. return status;
  2131. }
  2132. static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
  2133. struct qstr *name, struct nfs_fh *fhandle,
  2134. struct nfs_fattr *fattr)
  2135. {
  2136. struct nfs4_exception exception = { };
  2137. int err;
  2138. do {
  2139. err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
  2140. /* FIXME: !!!! */
  2141. if (err == -NFS4ERR_MOVED) {
  2142. err = -EREMOTE;
  2143. break;
  2144. }
  2145. err = nfs4_handle_exception(server, err, &exception);
  2146. } while (exception.retry);
  2147. return err;
  2148. }
  2149. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  2150. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2151. {
  2152. int status;
  2153. dprintk("NFS call lookup %s\n", name->name);
  2154. status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
  2155. if (status == -NFS4ERR_MOVED)
  2156. status = nfs4_get_referral(dir, name, fattr, fhandle);
  2157. dprintk("NFS reply lookup: %d\n", status);
  2158. return status;
  2159. }
  2160. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2161. {
  2162. struct nfs4_exception exception = { };
  2163. int err;
  2164. do {
  2165. err = nfs4_handle_exception(NFS_SERVER(dir),
  2166. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  2167. &exception);
  2168. } while (exception.retry);
  2169. return err;
  2170. }
  2171. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2172. {
  2173. struct nfs_server *server = NFS_SERVER(inode);
  2174. struct nfs4_accessargs args = {
  2175. .fh = NFS_FH(inode),
  2176. .bitmask = server->attr_bitmask,
  2177. };
  2178. struct nfs4_accessres res = {
  2179. .server = server,
  2180. };
  2181. struct rpc_message msg = {
  2182. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2183. .rpc_argp = &args,
  2184. .rpc_resp = &res,
  2185. .rpc_cred = entry->cred,
  2186. };
  2187. int mode = entry->mask;
  2188. int status;
  2189. /*
  2190. * Determine which access bits we want to ask for...
  2191. */
  2192. if (mode & MAY_READ)
  2193. args.access |= NFS4_ACCESS_READ;
  2194. if (S_ISDIR(inode->i_mode)) {
  2195. if (mode & MAY_WRITE)
  2196. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2197. if (mode & MAY_EXEC)
  2198. args.access |= NFS4_ACCESS_LOOKUP;
  2199. } else {
  2200. if (mode & MAY_WRITE)
  2201. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2202. if (mode & MAY_EXEC)
  2203. args.access |= NFS4_ACCESS_EXECUTE;
  2204. }
  2205. res.fattr = nfs_alloc_fattr();
  2206. if (res.fattr == NULL)
  2207. return -ENOMEM;
  2208. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2209. if (!status) {
  2210. entry->mask = 0;
  2211. if (res.access & NFS4_ACCESS_READ)
  2212. entry->mask |= MAY_READ;
  2213. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2214. entry->mask |= MAY_WRITE;
  2215. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2216. entry->mask |= MAY_EXEC;
  2217. nfs_refresh_inode(inode, res.fattr);
  2218. }
  2219. nfs_free_fattr(res.fattr);
  2220. return status;
  2221. }
  2222. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2223. {
  2224. struct nfs4_exception exception = { };
  2225. int err;
  2226. do {
  2227. err = nfs4_handle_exception(NFS_SERVER(inode),
  2228. _nfs4_proc_access(inode, entry),
  2229. &exception);
  2230. } while (exception.retry);
  2231. return err;
  2232. }
  2233. /*
  2234. * TODO: For the time being, we don't try to get any attributes
  2235. * along with any of the zero-copy operations READ, READDIR,
  2236. * READLINK, WRITE.
  2237. *
  2238. * In the case of the first three, we want to put the GETATTR
  2239. * after the read-type operation -- this is because it is hard
  2240. * to predict the length of a GETATTR response in v4, and thus
  2241. * align the READ data correctly. This means that the GETATTR
  2242. * may end up partially falling into the page cache, and we should
  2243. * shift it into the 'tail' of the xdr_buf before processing.
  2244. * To do this efficiently, we need to know the total length
  2245. * of data received, which doesn't seem to be available outside
  2246. * of the RPC layer.
  2247. *
  2248. * In the case of WRITE, we also want to put the GETATTR after
  2249. * the operation -- in this case because we want to make sure
  2250. * we get the post-operation mtime and size. This means that
  2251. * we can't use xdr_encode_pages() as written: we need a variant
  2252. * of it which would leave room in the 'tail' iovec.
  2253. *
  2254. * Both of these changes to the XDR layer would in fact be quite
  2255. * minor, but I decided to leave them for a subsequent patch.
  2256. */
  2257. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2258. unsigned int pgbase, unsigned int pglen)
  2259. {
  2260. struct nfs4_readlink args = {
  2261. .fh = NFS_FH(inode),
  2262. .pgbase = pgbase,
  2263. .pglen = pglen,
  2264. .pages = &page,
  2265. };
  2266. struct nfs4_readlink_res res;
  2267. struct rpc_message msg = {
  2268. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2269. .rpc_argp = &args,
  2270. .rpc_resp = &res,
  2271. };
  2272. return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2273. }
  2274. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2275. unsigned int pgbase, unsigned int pglen)
  2276. {
  2277. struct nfs4_exception exception = { };
  2278. int err;
  2279. do {
  2280. err = nfs4_handle_exception(NFS_SERVER(inode),
  2281. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2282. &exception);
  2283. } while (exception.retry);
  2284. return err;
  2285. }
  2286. /*
  2287. * Got race?
  2288. * We will need to arrange for the VFS layer to provide an atomic open.
  2289. * Until then, this create/open method is prone to inefficiency and race
  2290. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2291. * placed on the wire.
  2292. *
  2293. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2294. * The file will be opened again in the subsequent VFS open call
  2295. * (nfs4_proc_file_open).
  2296. *
  2297. * The open for read will just hang around to be used by any process that
  2298. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2299. */
  2300. static int
  2301. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2302. int flags, struct nameidata *nd)
  2303. {
  2304. struct path path = {
  2305. .mnt = nd->path.mnt,
  2306. .dentry = dentry,
  2307. };
  2308. struct nfs4_state *state;
  2309. struct rpc_cred *cred;
  2310. fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
  2311. int status = 0;
  2312. cred = rpc_lookup_cred();
  2313. if (IS_ERR(cred)) {
  2314. status = PTR_ERR(cred);
  2315. goto out;
  2316. }
  2317. state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
  2318. d_drop(dentry);
  2319. if (IS_ERR(state)) {
  2320. status = PTR_ERR(state);
  2321. goto out_putcred;
  2322. }
  2323. d_add(dentry, igrab(state->inode));
  2324. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2325. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  2326. status = nfs4_intent_set_file(nd, &path, state, fmode);
  2327. else
  2328. nfs4_close_sync(&path, state, fmode);
  2329. out_putcred:
  2330. put_rpccred(cred);
  2331. out:
  2332. return status;
  2333. }
  2334. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2335. {
  2336. struct nfs_server *server = NFS_SERVER(dir);
  2337. struct nfs_removeargs args = {
  2338. .fh = NFS_FH(dir),
  2339. .name.len = name->len,
  2340. .name.name = name->name,
  2341. .bitmask = server->attr_bitmask,
  2342. };
  2343. struct nfs_removeres res = {
  2344. .server = server,
  2345. };
  2346. struct rpc_message msg = {
  2347. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2348. .rpc_argp = &args,
  2349. .rpc_resp = &res,
  2350. };
  2351. int status = -ENOMEM;
  2352. res.dir_attr = nfs_alloc_fattr();
  2353. if (res.dir_attr == NULL)
  2354. goto out;
  2355. status = nfs4_call_sync(server, &msg, &args, &res, 1);
  2356. if (status == 0) {
  2357. update_changeattr(dir, &res.cinfo);
  2358. nfs_post_op_update_inode(dir, res.dir_attr);
  2359. }
  2360. nfs_free_fattr(res.dir_attr);
  2361. out:
  2362. return status;
  2363. }
  2364. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2365. {
  2366. struct nfs4_exception exception = { };
  2367. int err;
  2368. do {
  2369. err = nfs4_handle_exception(NFS_SERVER(dir),
  2370. _nfs4_proc_remove(dir, name),
  2371. &exception);
  2372. } while (exception.retry);
  2373. return err;
  2374. }
  2375. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2376. {
  2377. struct nfs_server *server = NFS_SERVER(dir);
  2378. struct nfs_removeargs *args = msg->rpc_argp;
  2379. struct nfs_removeres *res = msg->rpc_resp;
  2380. args->bitmask = server->cache_consistency_bitmask;
  2381. res->server = server;
  2382. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2383. }
  2384. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2385. {
  2386. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2387. nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
  2388. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2389. return 0;
  2390. update_changeattr(dir, &res->cinfo);
  2391. nfs_post_op_update_inode(dir, res->dir_attr);
  2392. return 1;
  2393. }
  2394. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2395. struct inode *new_dir, struct qstr *new_name)
  2396. {
  2397. struct nfs_server *server = NFS_SERVER(old_dir);
  2398. struct nfs4_rename_arg arg = {
  2399. .old_dir = NFS_FH(old_dir),
  2400. .new_dir = NFS_FH(new_dir),
  2401. .old_name = old_name,
  2402. .new_name = new_name,
  2403. .bitmask = server->attr_bitmask,
  2404. };
  2405. struct nfs4_rename_res res = {
  2406. .server = server,
  2407. };
  2408. struct rpc_message msg = {
  2409. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2410. .rpc_argp = &arg,
  2411. .rpc_resp = &res,
  2412. };
  2413. int status = -ENOMEM;
  2414. res.old_fattr = nfs_alloc_fattr();
  2415. res.new_fattr = nfs_alloc_fattr();
  2416. if (res.old_fattr == NULL || res.new_fattr == NULL)
  2417. goto out;
  2418. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2419. if (!status) {
  2420. update_changeattr(old_dir, &res.old_cinfo);
  2421. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2422. update_changeattr(new_dir, &res.new_cinfo);
  2423. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2424. }
  2425. out:
  2426. nfs_free_fattr(res.new_fattr);
  2427. nfs_free_fattr(res.old_fattr);
  2428. return status;
  2429. }
  2430. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2431. struct inode *new_dir, struct qstr *new_name)
  2432. {
  2433. struct nfs4_exception exception = { };
  2434. int err;
  2435. do {
  2436. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2437. _nfs4_proc_rename(old_dir, old_name,
  2438. new_dir, new_name),
  2439. &exception);
  2440. } while (exception.retry);
  2441. return err;
  2442. }
  2443. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2444. {
  2445. struct nfs_server *server = NFS_SERVER(inode);
  2446. struct nfs4_link_arg arg = {
  2447. .fh = NFS_FH(inode),
  2448. .dir_fh = NFS_FH(dir),
  2449. .name = name,
  2450. .bitmask = server->attr_bitmask,
  2451. };
  2452. struct nfs4_link_res res = {
  2453. .server = server,
  2454. };
  2455. struct rpc_message msg = {
  2456. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2457. .rpc_argp = &arg,
  2458. .rpc_resp = &res,
  2459. };
  2460. int status = -ENOMEM;
  2461. res.fattr = nfs_alloc_fattr();
  2462. res.dir_attr = nfs_alloc_fattr();
  2463. if (res.fattr == NULL || res.dir_attr == NULL)
  2464. goto out;
  2465. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2466. if (!status) {
  2467. update_changeattr(dir, &res.cinfo);
  2468. nfs_post_op_update_inode(dir, res.dir_attr);
  2469. nfs_post_op_update_inode(inode, res.fattr);
  2470. }
  2471. out:
  2472. nfs_free_fattr(res.dir_attr);
  2473. nfs_free_fattr(res.fattr);
  2474. return status;
  2475. }
  2476. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2477. {
  2478. struct nfs4_exception exception = { };
  2479. int err;
  2480. do {
  2481. err = nfs4_handle_exception(NFS_SERVER(inode),
  2482. _nfs4_proc_link(inode, dir, name),
  2483. &exception);
  2484. } while (exception.retry);
  2485. return err;
  2486. }
  2487. struct nfs4_createdata {
  2488. struct rpc_message msg;
  2489. struct nfs4_create_arg arg;
  2490. struct nfs4_create_res res;
  2491. struct nfs_fh fh;
  2492. struct nfs_fattr fattr;
  2493. struct nfs_fattr dir_fattr;
  2494. };
  2495. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2496. struct qstr *name, struct iattr *sattr, u32 ftype)
  2497. {
  2498. struct nfs4_createdata *data;
  2499. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2500. if (data != NULL) {
  2501. struct nfs_server *server = NFS_SERVER(dir);
  2502. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2503. data->msg.rpc_argp = &data->arg;
  2504. data->msg.rpc_resp = &data->res;
  2505. data->arg.dir_fh = NFS_FH(dir);
  2506. data->arg.server = server;
  2507. data->arg.name = name;
  2508. data->arg.attrs = sattr;
  2509. data->arg.ftype = ftype;
  2510. data->arg.bitmask = server->attr_bitmask;
  2511. data->res.server = server;
  2512. data->res.fh = &data->fh;
  2513. data->res.fattr = &data->fattr;
  2514. data->res.dir_fattr = &data->dir_fattr;
  2515. nfs_fattr_init(data->res.fattr);
  2516. nfs_fattr_init(data->res.dir_fattr);
  2517. }
  2518. return data;
  2519. }
  2520. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2521. {
  2522. int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
  2523. &data->arg, &data->res, 1);
  2524. if (status == 0) {
  2525. update_changeattr(dir, &data->res.dir_cinfo);
  2526. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2527. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2528. }
  2529. return status;
  2530. }
  2531. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2532. {
  2533. kfree(data);
  2534. }
  2535. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2536. struct page *page, unsigned int len, struct iattr *sattr)
  2537. {
  2538. struct nfs4_createdata *data;
  2539. int status = -ENAMETOOLONG;
  2540. if (len > NFS4_MAXPATHLEN)
  2541. goto out;
  2542. status = -ENOMEM;
  2543. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2544. if (data == NULL)
  2545. goto out;
  2546. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2547. data->arg.u.symlink.pages = &page;
  2548. data->arg.u.symlink.len = len;
  2549. status = nfs4_do_create(dir, dentry, data);
  2550. nfs4_free_createdata(data);
  2551. out:
  2552. return status;
  2553. }
  2554. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2555. struct page *page, unsigned int len, struct iattr *sattr)
  2556. {
  2557. struct nfs4_exception exception = { };
  2558. int err;
  2559. do {
  2560. err = nfs4_handle_exception(NFS_SERVER(dir),
  2561. _nfs4_proc_symlink(dir, dentry, page,
  2562. len, sattr),
  2563. &exception);
  2564. } while (exception.retry);
  2565. return err;
  2566. }
  2567. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2568. struct iattr *sattr)
  2569. {
  2570. struct nfs4_createdata *data;
  2571. int status = -ENOMEM;
  2572. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2573. if (data == NULL)
  2574. goto out;
  2575. status = nfs4_do_create(dir, dentry, data);
  2576. nfs4_free_createdata(data);
  2577. out:
  2578. return status;
  2579. }
  2580. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2581. struct iattr *sattr)
  2582. {
  2583. struct nfs4_exception exception = { };
  2584. int err;
  2585. do {
  2586. err = nfs4_handle_exception(NFS_SERVER(dir),
  2587. _nfs4_proc_mkdir(dir, dentry, sattr),
  2588. &exception);
  2589. } while (exception.retry);
  2590. return err;
  2591. }
  2592. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2593. u64 cookie, struct page *page, unsigned int count, int plus)
  2594. {
  2595. struct inode *dir = dentry->d_inode;
  2596. struct nfs4_readdir_arg args = {
  2597. .fh = NFS_FH(dir),
  2598. .pages = &page,
  2599. .pgbase = 0,
  2600. .count = count,
  2601. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2602. };
  2603. struct nfs4_readdir_res res;
  2604. struct rpc_message msg = {
  2605. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2606. .rpc_argp = &args,
  2607. .rpc_resp = &res,
  2608. .rpc_cred = cred,
  2609. };
  2610. int status;
  2611. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2612. dentry->d_parent->d_name.name,
  2613. dentry->d_name.name,
  2614. (unsigned long long)cookie);
  2615. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2616. res.pgbase = args.pgbase;
  2617. status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
  2618. if (status == 0)
  2619. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2620. nfs_invalidate_atime(dir);
  2621. dprintk("%s: returns %d\n", __func__, status);
  2622. return status;
  2623. }
  2624. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2625. u64 cookie, struct page *page, unsigned int count, int plus)
  2626. {
  2627. struct nfs4_exception exception = { };
  2628. int err;
  2629. do {
  2630. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2631. _nfs4_proc_readdir(dentry, cred, cookie,
  2632. page, count, plus),
  2633. &exception);
  2634. } while (exception.retry);
  2635. return err;
  2636. }
  2637. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2638. struct iattr *sattr, dev_t rdev)
  2639. {
  2640. struct nfs4_createdata *data;
  2641. int mode = sattr->ia_mode;
  2642. int status = -ENOMEM;
  2643. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2644. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2645. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2646. if (data == NULL)
  2647. goto out;
  2648. if (S_ISFIFO(mode))
  2649. data->arg.ftype = NF4FIFO;
  2650. else if (S_ISBLK(mode)) {
  2651. data->arg.ftype = NF4BLK;
  2652. data->arg.u.device.specdata1 = MAJOR(rdev);
  2653. data->arg.u.device.specdata2 = MINOR(rdev);
  2654. }
  2655. else if (S_ISCHR(mode)) {
  2656. data->arg.ftype = NF4CHR;
  2657. data->arg.u.device.specdata1 = MAJOR(rdev);
  2658. data->arg.u.device.specdata2 = MINOR(rdev);
  2659. }
  2660. status = nfs4_do_create(dir, dentry, data);
  2661. nfs4_free_createdata(data);
  2662. out:
  2663. return status;
  2664. }
  2665. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2666. struct iattr *sattr, dev_t rdev)
  2667. {
  2668. struct nfs4_exception exception = { };
  2669. int err;
  2670. do {
  2671. err = nfs4_handle_exception(NFS_SERVER(dir),
  2672. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2673. &exception);
  2674. } while (exception.retry);
  2675. return err;
  2676. }
  2677. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2678. struct nfs_fsstat *fsstat)
  2679. {
  2680. struct nfs4_statfs_arg args = {
  2681. .fh = fhandle,
  2682. .bitmask = server->attr_bitmask,
  2683. };
  2684. struct nfs4_statfs_res res = {
  2685. .fsstat = fsstat,
  2686. };
  2687. struct rpc_message msg = {
  2688. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2689. .rpc_argp = &args,
  2690. .rpc_resp = &res,
  2691. };
  2692. nfs_fattr_init(fsstat->fattr);
  2693. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2694. }
  2695. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2696. {
  2697. struct nfs4_exception exception = { };
  2698. int err;
  2699. do {
  2700. err = nfs4_handle_exception(server,
  2701. _nfs4_proc_statfs(server, fhandle, fsstat),
  2702. &exception);
  2703. } while (exception.retry);
  2704. return err;
  2705. }
  2706. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2707. struct nfs_fsinfo *fsinfo)
  2708. {
  2709. struct nfs4_fsinfo_arg args = {
  2710. .fh = fhandle,
  2711. .bitmask = server->attr_bitmask,
  2712. };
  2713. struct nfs4_fsinfo_res res = {
  2714. .fsinfo = fsinfo,
  2715. };
  2716. struct rpc_message msg = {
  2717. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2718. .rpc_argp = &args,
  2719. .rpc_resp = &res,
  2720. };
  2721. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2722. }
  2723. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2724. {
  2725. struct nfs4_exception exception = { };
  2726. int err;
  2727. do {
  2728. err = nfs4_handle_exception(server,
  2729. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2730. &exception);
  2731. } while (exception.retry);
  2732. return err;
  2733. }
  2734. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2735. {
  2736. nfs_fattr_init(fsinfo->fattr);
  2737. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2738. }
  2739. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2740. struct nfs_pathconf *pathconf)
  2741. {
  2742. struct nfs4_pathconf_arg args = {
  2743. .fh = fhandle,
  2744. .bitmask = server->attr_bitmask,
  2745. };
  2746. struct nfs4_pathconf_res res = {
  2747. .pathconf = pathconf,
  2748. };
  2749. struct rpc_message msg = {
  2750. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2751. .rpc_argp = &args,
  2752. .rpc_resp = &res,
  2753. };
  2754. /* None of the pathconf attributes are mandatory to implement */
  2755. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2756. memset(pathconf, 0, sizeof(*pathconf));
  2757. return 0;
  2758. }
  2759. nfs_fattr_init(pathconf->fattr);
  2760. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2761. }
  2762. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2763. struct nfs_pathconf *pathconf)
  2764. {
  2765. struct nfs4_exception exception = { };
  2766. int err;
  2767. do {
  2768. err = nfs4_handle_exception(server,
  2769. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2770. &exception);
  2771. } while (exception.retry);
  2772. return err;
  2773. }
  2774. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2775. {
  2776. struct nfs_server *server = NFS_SERVER(data->inode);
  2777. dprintk("--> %s\n", __func__);
  2778. nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
  2779. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  2780. nfs_restart_rpc(task, server->nfs_client);
  2781. return -EAGAIN;
  2782. }
  2783. nfs_invalidate_atime(data->inode);
  2784. if (task->tk_status > 0)
  2785. renew_lease(server, data->timestamp);
  2786. return 0;
  2787. }
  2788. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  2789. {
  2790. data->timestamp = jiffies;
  2791. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  2792. }
  2793. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2794. {
  2795. struct inode *inode = data->inode;
  2796. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2797. task->tk_status);
  2798. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  2799. nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2800. return -EAGAIN;
  2801. }
  2802. if (task->tk_status >= 0) {
  2803. renew_lease(NFS_SERVER(inode), data->timestamp);
  2804. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2805. }
  2806. return 0;
  2807. }
  2808. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2809. {
  2810. struct nfs_server *server = NFS_SERVER(data->inode);
  2811. data->args.bitmask = server->cache_consistency_bitmask;
  2812. data->res.server = server;
  2813. data->timestamp = jiffies;
  2814. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  2815. }
  2816. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2817. {
  2818. struct inode *inode = data->inode;
  2819. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2820. task->tk_status);
  2821. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  2822. nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2823. return -EAGAIN;
  2824. }
  2825. nfs_refresh_inode(inode, data->res.fattr);
  2826. return 0;
  2827. }
  2828. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2829. {
  2830. struct nfs_server *server = NFS_SERVER(data->inode);
  2831. data->args.bitmask = server->cache_consistency_bitmask;
  2832. data->res.server = server;
  2833. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  2834. }
  2835. struct nfs4_renewdata {
  2836. struct nfs_client *client;
  2837. unsigned long timestamp;
  2838. };
  2839. /*
  2840. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2841. * standalone procedure for queueing an asynchronous RENEW.
  2842. */
  2843. static void nfs4_renew_release(void *calldata)
  2844. {
  2845. struct nfs4_renewdata *data = calldata;
  2846. struct nfs_client *clp = data->client;
  2847. if (atomic_read(&clp->cl_count) > 1)
  2848. nfs4_schedule_state_renewal(clp);
  2849. nfs_put_client(clp);
  2850. kfree(data);
  2851. }
  2852. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  2853. {
  2854. struct nfs4_renewdata *data = calldata;
  2855. struct nfs_client *clp = data->client;
  2856. unsigned long timestamp = data->timestamp;
  2857. if (task->tk_status < 0) {
  2858. /* Unless we're shutting down, schedule state recovery! */
  2859. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
  2860. nfs4_schedule_state_recovery(clp);
  2861. return;
  2862. }
  2863. spin_lock(&clp->cl_lock);
  2864. if (time_before(clp->cl_last_renewal,timestamp))
  2865. clp->cl_last_renewal = timestamp;
  2866. spin_unlock(&clp->cl_lock);
  2867. }
  2868. static const struct rpc_call_ops nfs4_renew_ops = {
  2869. .rpc_call_done = nfs4_renew_done,
  2870. .rpc_release = nfs4_renew_release,
  2871. };
  2872. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2873. {
  2874. struct rpc_message msg = {
  2875. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2876. .rpc_argp = clp,
  2877. .rpc_cred = cred,
  2878. };
  2879. struct nfs4_renewdata *data;
  2880. if (!atomic_inc_not_zero(&clp->cl_count))
  2881. return -EIO;
  2882. data = kmalloc(sizeof(*data), GFP_KERNEL);
  2883. if (data == NULL)
  2884. return -ENOMEM;
  2885. data->client = clp;
  2886. data->timestamp = jiffies;
  2887. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2888. &nfs4_renew_ops, data);
  2889. }
  2890. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2891. {
  2892. struct rpc_message msg = {
  2893. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2894. .rpc_argp = clp,
  2895. .rpc_cred = cred,
  2896. };
  2897. unsigned long now = jiffies;
  2898. int status;
  2899. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2900. if (status < 0)
  2901. return status;
  2902. spin_lock(&clp->cl_lock);
  2903. if (time_before(clp->cl_last_renewal,now))
  2904. clp->cl_last_renewal = now;
  2905. spin_unlock(&clp->cl_lock);
  2906. return 0;
  2907. }
  2908. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2909. {
  2910. return (server->caps & NFS_CAP_ACLS)
  2911. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2912. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2913. }
  2914. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2915. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2916. * the stack.
  2917. */
  2918. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2919. static void buf_to_pages(const void *buf, size_t buflen,
  2920. struct page **pages, unsigned int *pgbase)
  2921. {
  2922. const void *p = buf;
  2923. *pgbase = offset_in_page(buf);
  2924. p -= *pgbase;
  2925. while (p < buf + buflen) {
  2926. *(pages++) = virt_to_page(p);
  2927. p += PAGE_CACHE_SIZE;
  2928. }
  2929. }
  2930. struct nfs4_cached_acl {
  2931. int cached;
  2932. size_t len;
  2933. char data[0];
  2934. };
  2935. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2936. {
  2937. struct nfs_inode *nfsi = NFS_I(inode);
  2938. spin_lock(&inode->i_lock);
  2939. kfree(nfsi->nfs4_acl);
  2940. nfsi->nfs4_acl = acl;
  2941. spin_unlock(&inode->i_lock);
  2942. }
  2943. static void nfs4_zap_acl_attr(struct inode *inode)
  2944. {
  2945. nfs4_set_cached_acl(inode, NULL);
  2946. }
  2947. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2948. {
  2949. struct nfs_inode *nfsi = NFS_I(inode);
  2950. struct nfs4_cached_acl *acl;
  2951. int ret = -ENOENT;
  2952. spin_lock(&inode->i_lock);
  2953. acl = nfsi->nfs4_acl;
  2954. if (acl == NULL)
  2955. goto out;
  2956. if (buf == NULL) /* user is just asking for length */
  2957. goto out_len;
  2958. if (acl->cached == 0)
  2959. goto out;
  2960. ret = -ERANGE; /* see getxattr(2) man page */
  2961. if (acl->len > buflen)
  2962. goto out;
  2963. memcpy(buf, acl->data, acl->len);
  2964. out_len:
  2965. ret = acl->len;
  2966. out:
  2967. spin_unlock(&inode->i_lock);
  2968. return ret;
  2969. }
  2970. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2971. {
  2972. struct nfs4_cached_acl *acl;
  2973. if (buf && acl_len <= PAGE_SIZE) {
  2974. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2975. if (acl == NULL)
  2976. goto out;
  2977. acl->cached = 1;
  2978. memcpy(acl->data, buf, acl_len);
  2979. } else {
  2980. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2981. if (acl == NULL)
  2982. goto out;
  2983. acl->cached = 0;
  2984. }
  2985. acl->len = acl_len;
  2986. out:
  2987. nfs4_set_cached_acl(inode, acl);
  2988. }
  2989. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2990. {
  2991. struct page *pages[NFS4ACL_MAXPAGES];
  2992. struct nfs_getaclargs args = {
  2993. .fh = NFS_FH(inode),
  2994. .acl_pages = pages,
  2995. .acl_len = buflen,
  2996. };
  2997. struct nfs_getaclres res = {
  2998. .acl_len = buflen,
  2999. };
  3000. void *resp_buf;
  3001. struct rpc_message msg = {
  3002. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  3003. .rpc_argp = &args,
  3004. .rpc_resp = &res,
  3005. };
  3006. struct page *localpage = NULL;
  3007. int ret;
  3008. if (buflen < PAGE_SIZE) {
  3009. /* As long as we're doing a round trip to the server anyway,
  3010. * let's be prepared for a page of acl data. */
  3011. localpage = alloc_page(GFP_KERNEL);
  3012. resp_buf = page_address(localpage);
  3013. if (localpage == NULL)
  3014. return -ENOMEM;
  3015. args.acl_pages[0] = localpage;
  3016. args.acl_pgbase = 0;
  3017. args.acl_len = PAGE_SIZE;
  3018. } else {
  3019. resp_buf = buf;
  3020. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  3021. }
  3022. ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  3023. if (ret)
  3024. goto out_free;
  3025. if (res.acl_len > args.acl_len)
  3026. nfs4_write_cached_acl(inode, NULL, res.acl_len);
  3027. else
  3028. nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
  3029. if (buf) {
  3030. ret = -ERANGE;
  3031. if (res.acl_len > buflen)
  3032. goto out_free;
  3033. if (localpage)
  3034. memcpy(buf, resp_buf, res.acl_len);
  3035. }
  3036. ret = res.acl_len;
  3037. out_free:
  3038. if (localpage)
  3039. __free_page(localpage);
  3040. return ret;
  3041. }
  3042. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3043. {
  3044. struct nfs4_exception exception = { };
  3045. ssize_t ret;
  3046. do {
  3047. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3048. if (ret >= 0)
  3049. break;
  3050. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3051. } while (exception.retry);
  3052. return ret;
  3053. }
  3054. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3055. {
  3056. struct nfs_server *server = NFS_SERVER(inode);
  3057. int ret;
  3058. if (!nfs4_server_supports_acls(server))
  3059. return -EOPNOTSUPP;
  3060. ret = nfs_revalidate_inode(server, inode);
  3061. if (ret < 0)
  3062. return ret;
  3063. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3064. if (ret != -ENOENT)
  3065. return ret;
  3066. return nfs4_get_acl_uncached(inode, buf, buflen);
  3067. }
  3068. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3069. {
  3070. struct nfs_server *server = NFS_SERVER(inode);
  3071. struct page *pages[NFS4ACL_MAXPAGES];
  3072. struct nfs_setaclargs arg = {
  3073. .fh = NFS_FH(inode),
  3074. .acl_pages = pages,
  3075. .acl_len = buflen,
  3076. };
  3077. struct nfs_setaclres res;
  3078. struct rpc_message msg = {
  3079. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3080. .rpc_argp = &arg,
  3081. .rpc_resp = &res,
  3082. };
  3083. int ret;
  3084. if (!nfs4_server_supports_acls(server))
  3085. return -EOPNOTSUPP;
  3086. nfs_inode_return_delegation(inode);
  3087. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3088. ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3089. nfs_access_zap_cache(inode);
  3090. nfs_zap_acl_cache(inode);
  3091. return ret;
  3092. }
  3093. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3094. {
  3095. struct nfs4_exception exception = { };
  3096. int err;
  3097. do {
  3098. err = nfs4_handle_exception(NFS_SERVER(inode),
  3099. __nfs4_proc_set_acl(inode, buf, buflen),
  3100. &exception);
  3101. } while (exception.retry);
  3102. return err;
  3103. }
  3104. static int
  3105. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3106. {
  3107. struct nfs_client *clp = server->nfs_client;
  3108. if (task->tk_status >= 0)
  3109. return 0;
  3110. switch(task->tk_status) {
  3111. case -NFS4ERR_ADMIN_REVOKED:
  3112. case -NFS4ERR_BAD_STATEID:
  3113. case -NFS4ERR_OPENMODE:
  3114. if (state == NULL)
  3115. break;
  3116. nfs4_state_mark_reclaim_nograce(clp, state);
  3117. goto do_state_recovery;
  3118. case -NFS4ERR_STALE_STATEID:
  3119. if (state == NULL)
  3120. break;
  3121. nfs4_state_mark_reclaim_reboot(clp, state);
  3122. case -NFS4ERR_STALE_CLIENTID:
  3123. case -NFS4ERR_EXPIRED:
  3124. goto do_state_recovery;
  3125. #if defined(CONFIG_NFS_V4_1)
  3126. case -NFS4ERR_BADSESSION:
  3127. case -NFS4ERR_BADSLOT:
  3128. case -NFS4ERR_BAD_HIGH_SLOT:
  3129. case -NFS4ERR_DEADSESSION:
  3130. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3131. case -NFS4ERR_SEQ_FALSE_RETRY:
  3132. case -NFS4ERR_SEQ_MISORDERED:
  3133. dprintk("%s ERROR %d, Reset session\n", __func__,
  3134. task->tk_status);
  3135. nfs4_schedule_state_recovery(clp);
  3136. task->tk_status = 0;
  3137. return -EAGAIN;
  3138. #endif /* CONFIG_NFS_V4_1 */
  3139. case -NFS4ERR_DELAY:
  3140. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3141. case -NFS4ERR_GRACE:
  3142. case -EKEYEXPIRED:
  3143. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3144. task->tk_status = 0;
  3145. return -EAGAIN;
  3146. case -NFS4ERR_OLD_STATEID:
  3147. task->tk_status = 0;
  3148. return -EAGAIN;
  3149. }
  3150. task->tk_status = nfs4_map_errors(task->tk_status);
  3151. return 0;
  3152. do_state_recovery:
  3153. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3154. nfs4_schedule_state_recovery(clp);
  3155. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3156. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3157. task->tk_status = 0;
  3158. return -EAGAIN;
  3159. }
  3160. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  3161. unsigned short port, struct rpc_cred *cred,
  3162. struct nfs4_setclientid_res *res)
  3163. {
  3164. nfs4_verifier sc_verifier;
  3165. struct nfs4_setclientid setclientid = {
  3166. .sc_verifier = &sc_verifier,
  3167. .sc_prog = program,
  3168. };
  3169. struct rpc_message msg = {
  3170. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3171. .rpc_argp = &setclientid,
  3172. .rpc_resp = res,
  3173. .rpc_cred = cred,
  3174. };
  3175. __be32 *p;
  3176. int loop = 0;
  3177. int status;
  3178. p = (__be32*)sc_verifier.data;
  3179. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  3180. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  3181. for(;;) {
  3182. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  3183. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  3184. clp->cl_ipaddr,
  3185. rpc_peeraddr2str(clp->cl_rpcclient,
  3186. RPC_DISPLAY_ADDR),
  3187. rpc_peeraddr2str(clp->cl_rpcclient,
  3188. RPC_DISPLAY_PROTO),
  3189. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3190. clp->cl_id_uniquifier);
  3191. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3192. sizeof(setclientid.sc_netid),
  3193. rpc_peeraddr2str(clp->cl_rpcclient,
  3194. RPC_DISPLAY_NETID));
  3195. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3196. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3197. clp->cl_ipaddr, port >> 8, port & 255);
  3198. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3199. if (status != -NFS4ERR_CLID_INUSE)
  3200. break;
  3201. if (signalled())
  3202. break;
  3203. if (loop++ & 1)
  3204. ssleep(clp->cl_lease_time + 1);
  3205. else
  3206. if (++clp->cl_id_uniquifier == 0)
  3207. break;
  3208. }
  3209. return status;
  3210. }
  3211. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  3212. struct nfs4_setclientid_res *arg,
  3213. struct rpc_cred *cred)
  3214. {
  3215. struct nfs_fsinfo fsinfo;
  3216. struct rpc_message msg = {
  3217. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3218. .rpc_argp = arg,
  3219. .rpc_resp = &fsinfo,
  3220. .rpc_cred = cred,
  3221. };
  3222. unsigned long now;
  3223. int status;
  3224. now = jiffies;
  3225. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3226. if (status == 0) {
  3227. spin_lock(&clp->cl_lock);
  3228. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3229. clp->cl_last_renewal = now;
  3230. spin_unlock(&clp->cl_lock);
  3231. }
  3232. return status;
  3233. }
  3234. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  3235. struct nfs4_setclientid_res *arg,
  3236. struct rpc_cred *cred)
  3237. {
  3238. long timeout = 0;
  3239. int err;
  3240. do {
  3241. err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
  3242. switch (err) {
  3243. case 0:
  3244. return err;
  3245. case -NFS4ERR_RESOURCE:
  3246. /* The IBM lawyers misread another document! */
  3247. case -NFS4ERR_DELAY:
  3248. case -EKEYEXPIRED:
  3249. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  3250. }
  3251. } while (err == 0);
  3252. return err;
  3253. }
  3254. struct nfs4_delegreturndata {
  3255. struct nfs4_delegreturnargs args;
  3256. struct nfs4_delegreturnres res;
  3257. struct nfs_fh fh;
  3258. nfs4_stateid stateid;
  3259. unsigned long timestamp;
  3260. struct nfs_fattr fattr;
  3261. int rpc_status;
  3262. };
  3263. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3264. {
  3265. struct nfs4_delegreturndata *data = calldata;
  3266. nfs4_sequence_done(data->res.server, &data->res.seq_res,
  3267. task->tk_status);
  3268. switch (task->tk_status) {
  3269. case -NFS4ERR_STALE_STATEID:
  3270. case -NFS4ERR_EXPIRED:
  3271. case 0:
  3272. renew_lease(data->res.server, data->timestamp);
  3273. break;
  3274. default:
  3275. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3276. -EAGAIN) {
  3277. nfs_restart_rpc(task, data->res.server->nfs_client);
  3278. return;
  3279. }
  3280. }
  3281. data->rpc_status = task->tk_status;
  3282. }
  3283. static void nfs4_delegreturn_release(void *calldata)
  3284. {
  3285. kfree(calldata);
  3286. }
  3287. #if defined(CONFIG_NFS_V4_1)
  3288. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3289. {
  3290. struct nfs4_delegreturndata *d_data;
  3291. d_data = (struct nfs4_delegreturndata *)data;
  3292. if (nfs4_setup_sequence(d_data->res.server,
  3293. &d_data->args.seq_args,
  3294. &d_data->res.seq_res, 1, task))
  3295. return;
  3296. rpc_call_start(task);
  3297. }
  3298. #endif /* CONFIG_NFS_V4_1 */
  3299. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3300. #if defined(CONFIG_NFS_V4_1)
  3301. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3302. #endif /* CONFIG_NFS_V4_1 */
  3303. .rpc_call_done = nfs4_delegreturn_done,
  3304. .rpc_release = nfs4_delegreturn_release,
  3305. };
  3306. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3307. {
  3308. struct nfs4_delegreturndata *data;
  3309. struct nfs_server *server = NFS_SERVER(inode);
  3310. struct rpc_task *task;
  3311. struct rpc_message msg = {
  3312. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3313. .rpc_cred = cred,
  3314. };
  3315. struct rpc_task_setup task_setup_data = {
  3316. .rpc_client = server->client,
  3317. .rpc_message = &msg,
  3318. .callback_ops = &nfs4_delegreturn_ops,
  3319. .flags = RPC_TASK_ASYNC,
  3320. };
  3321. int status = 0;
  3322. data = kzalloc(sizeof(*data), GFP_NOFS);
  3323. if (data == NULL)
  3324. return -ENOMEM;
  3325. data->args.fhandle = &data->fh;
  3326. data->args.stateid = &data->stateid;
  3327. data->args.bitmask = server->attr_bitmask;
  3328. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3329. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  3330. data->res.fattr = &data->fattr;
  3331. data->res.server = server;
  3332. data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3333. nfs_fattr_init(data->res.fattr);
  3334. data->timestamp = jiffies;
  3335. data->rpc_status = 0;
  3336. task_setup_data.callback_data = data;
  3337. msg.rpc_argp = &data->args,
  3338. msg.rpc_resp = &data->res,
  3339. task = rpc_run_task(&task_setup_data);
  3340. if (IS_ERR(task))
  3341. return PTR_ERR(task);
  3342. if (!issync)
  3343. goto out;
  3344. status = nfs4_wait_for_completion_rpc_task(task);
  3345. if (status != 0)
  3346. goto out;
  3347. status = data->rpc_status;
  3348. if (status != 0)
  3349. goto out;
  3350. nfs_refresh_inode(inode, &data->fattr);
  3351. out:
  3352. rpc_put_task(task);
  3353. return status;
  3354. }
  3355. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3356. {
  3357. struct nfs_server *server = NFS_SERVER(inode);
  3358. struct nfs4_exception exception = { };
  3359. int err;
  3360. do {
  3361. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3362. switch (err) {
  3363. case -NFS4ERR_STALE_STATEID:
  3364. case -NFS4ERR_EXPIRED:
  3365. case 0:
  3366. return 0;
  3367. }
  3368. err = nfs4_handle_exception(server, err, &exception);
  3369. } while (exception.retry);
  3370. return err;
  3371. }
  3372. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3373. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3374. /*
  3375. * sleep, with exponential backoff, and retry the LOCK operation.
  3376. */
  3377. static unsigned long
  3378. nfs4_set_lock_task_retry(unsigned long timeout)
  3379. {
  3380. schedule_timeout_killable(timeout);
  3381. timeout <<= 1;
  3382. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3383. return NFS4_LOCK_MAXTIMEOUT;
  3384. return timeout;
  3385. }
  3386. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3387. {
  3388. struct inode *inode = state->inode;
  3389. struct nfs_server *server = NFS_SERVER(inode);
  3390. struct nfs_client *clp = server->nfs_client;
  3391. struct nfs_lockt_args arg = {
  3392. .fh = NFS_FH(inode),
  3393. .fl = request,
  3394. };
  3395. struct nfs_lockt_res res = {
  3396. .denied = request,
  3397. };
  3398. struct rpc_message msg = {
  3399. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3400. .rpc_argp = &arg,
  3401. .rpc_resp = &res,
  3402. .rpc_cred = state->owner->so_cred,
  3403. };
  3404. struct nfs4_lock_state *lsp;
  3405. int status;
  3406. arg.lock_owner.clientid = clp->cl_clientid;
  3407. status = nfs4_set_lock_state(state, request);
  3408. if (status != 0)
  3409. goto out;
  3410. lsp = request->fl_u.nfs4_fl.owner;
  3411. arg.lock_owner.id = lsp->ls_id.id;
  3412. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3413. switch (status) {
  3414. case 0:
  3415. request->fl_type = F_UNLCK;
  3416. break;
  3417. case -NFS4ERR_DENIED:
  3418. status = 0;
  3419. }
  3420. request->fl_ops->fl_release_private(request);
  3421. out:
  3422. return status;
  3423. }
  3424. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3425. {
  3426. struct nfs4_exception exception = { };
  3427. int err;
  3428. do {
  3429. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3430. _nfs4_proc_getlk(state, cmd, request),
  3431. &exception);
  3432. } while (exception.retry);
  3433. return err;
  3434. }
  3435. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3436. {
  3437. int res = 0;
  3438. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3439. case FL_POSIX:
  3440. res = posix_lock_file_wait(file, fl);
  3441. break;
  3442. case FL_FLOCK:
  3443. res = flock_lock_file_wait(file, fl);
  3444. break;
  3445. default:
  3446. BUG();
  3447. }
  3448. return res;
  3449. }
  3450. struct nfs4_unlockdata {
  3451. struct nfs_locku_args arg;
  3452. struct nfs_locku_res res;
  3453. struct nfs4_lock_state *lsp;
  3454. struct nfs_open_context *ctx;
  3455. struct file_lock fl;
  3456. const struct nfs_server *server;
  3457. unsigned long timestamp;
  3458. };
  3459. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3460. struct nfs_open_context *ctx,
  3461. struct nfs4_lock_state *lsp,
  3462. struct nfs_seqid *seqid)
  3463. {
  3464. struct nfs4_unlockdata *p;
  3465. struct inode *inode = lsp->ls_state->inode;
  3466. p = kzalloc(sizeof(*p), GFP_NOFS);
  3467. if (p == NULL)
  3468. return NULL;
  3469. p->arg.fh = NFS_FH(inode);
  3470. p->arg.fl = &p->fl;
  3471. p->arg.seqid = seqid;
  3472. p->res.seqid = seqid;
  3473. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3474. p->arg.stateid = &lsp->ls_stateid;
  3475. p->lsp = lsp;
  3476. atomic_inc(&lsp->ls_count);
  3477. /* Ensure we don't close file until we're done freeing locks! */
  3478. p->ctx = get_nfs_open_context(ctx);
  3479. memcpy(&p->fl, fl, sizeof(p->fl));
  3480. p->server = NFS_SERVER(inode);
  3481. return p;
  3482. }
  3483. static void nfs4_locku_release_calldata(void *data)
  3484. {
  3485. struct nfs4_unlockdata *calldata = data;
  3486. nfs_free_seqid(calldata->arg.seqid);
  3487. nfs4_put_lock_state(calldata->lsp);
  3488. put_nfs_open_context(calldata->ctx);
  3489. kfree(calldata);
  3490. }
  3491. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3492. {
  3493. struct nfs4_unlockdata *calldata = data;
  3494. nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
  3495. task->tk_status);
  3496. if (RPC_ASSASSINATED(task))
  3497. return;
  3498. switch (task->tk_status) {
  3499. case 0:
  3500. memcpy(calldata->lsp->ls_stateid.data,
  3501. calldata->res.stateid.data,
  3502. sizeof(calldata->lsp->ls_stateid.data));
  3503. renew_lease(calldata->server, calldata->timestamp);
  3504. break;
  3505. case -NFS4ERR_BAD_STATEID:
  3506. case -NFS4ERR_OLD_STATEID:
  3507. case -NFS4ERR_STALE_STATEID:
  3508. case -NFS4ERR_EXPIRED:
  3509. break;
  3510. default:
  3511. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3512. nfs_restart_rpc(task,
  3513. calldata->server->nfs_client);
  3514. }
  3515. }
  3516. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3517. {
  3518. struct nfs4_unlockdata *calldata = data;
  3519. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3520. return;
  3521. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3522. /* Note: exit _without_ running nfs4_locku_done */
  3523. task->tk_action = NULL;
  3524. return;
  3525. }
  3526. calldata->timestamp = jiffies;
  3527. if (nfs4_setup_sequence(calldata->server,
  3528. &calldata->arg.seq_args,
  3529. &calldata->res.seq_res, 1, task))
  3530. return;
  3531. rpc_call_start(task);
  3532. }
  3533. static const struct rpc_call_ops nfs4_locku_ops = {
  3534. .rpc_call_prepare = nfs4_locku_prepare,
  3535. .rpc_call_done = nfs4_locku_done,
  3536. .rpc_release = nfs4_locku_release_calldata,
  3537. };
  3538. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3539. struct nfs_open_context *ctx,
  3540. struct nfs4_lock_state *lsp,
  3541. struct nfs_seqid *seqid)
  3542. {
  3543. struct nfs4_unlockdata *data;
  3544. struct rpc_message msg = {
  3545. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3546. .rpc_cred = ctx->cred,
  3547. };
  3548. struct rpc_task_setup task_setup_data = {
  3549. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3550. .rpc_message = &msg,
  3551. .callback_ops = &nfs4_locku_ops,
  3552. .workqueue = nfsiod_workqueue,
  3553. .flags = RPC_TASK_ASYNC,
  3554. };
  3555. /* Ensure this is an unlock - when canceling a lock, the
  3556. * canceled lock is passed in, and it won't be an unlock.
  3557. */
  3558. fl->fl_type = F_UNLCK;
  3559. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3560. if (data == NULL) {
  3561. nfs_free_seqid(seqid);
  3562. return ERR_PTR(-ENOMEM);
  3563. }
  3564. msg.rpc_argp = &data->arg,
  3565. msg.rpc_resp = &data->res,
  3566. task_setup_data.callback_data = data;
  3567. return rpc_run_task(&task_setup_data);
  3568. }
  3569. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3570. {
  3571. struct nfs_inode *nfsi = NFS_I(state->inode);
  3572. struct nfs_seqid *seqid;
  3573. struct nfs4_lock_state *lsp;
  3574. struct rpc_task *task;
  3575. int status = 0;
  3576. unsigned char fl_flags = request->fl_flags;
  3577. status = nfs4_set_lock_state(state, request);
  3578. /* Unlock _before_ we do the RPC call */
  3579. request->fl_flags |= FL_EXISTS;
  3580. down_read(&nfsi->rwsem);
  3581. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3582. up_read(&nfsi->rwsem);
  3583. goto out;
  3584. }
  3585. up_read(&nfsi->rwsem);
  3586. if (status != 0)
  3587. goto out;
  3588. /* Is this a delegated lock? */
  3589. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3590. goto out;
  3591. lsp = request->fl_u.nfs4_fl.owner;
  3592. seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  3593. status = -ENOMEM;
  3594. if (seqid == NULL)
  3595. goto out;
  3596. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3597. status = PTR_ERR(task);
  3598. if (IS_ERR(task))
  3599. goto out;
  3600. status = nfs4_wait_for_completion_rpc_task(task);
  3601. rpc_put_task(task);
  3602. out:
  3603. request->fl_flags = fl_flags;
  3604. return status;
  3605. }
  3606. struct nfs4_lockdata {
  3607. struct nfs_lock_args arg;
  3608. struct nfs_lock_res res;
  3609. struct nfs4_lock_state *lsp;
  3610. struct nfs_open_context *ctx;
  3611. struct file_lock fl;
  3612. unsigned long timestamp;
  3613. int rpc_status;
  3614. int cancelled;
  3615. struct nfs_server *server;
  3616. };
  3617. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3618. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  3619. gfp_t gfp_mask)
  3620. {
  3621. struct nfs4_lockdata *p;
  3622. struct inode *inode = lsp->ls_state->inode;
  3623. struct nfs_server *server = NFS_SERVER(inode);
  3624. p = kzalloc(sizeof(*p), gfp_mask);
  3625. if (p == NULL)
  3626. return NULL;
  3627. p->arg.fh = NFS_FH(inode);
  3628. p->arg.fl = &p->fl;
  3629. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  3630. if (p->arg.open_seqid == NULL)
  3631. goto out_free;
  3632. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
  3633. if (p->arg.lock_seqid == NULL)
  3634. goto out_free_seqid;
  3635. p->arg.lock_stateid = &lsp->ls_stateid;
  3636. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3637. p->arg.lock_owner.id = lsp->ls_id.id;
  3638. p->res.lock_seqid = p->arg.lock_seqid;
  3639. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3640. p->lsp = lsp;
  3641. p->server = server;
  3642. atomic_inc(&lsp->ls_count);
  3643. p->ctx = get_nfs_open_context(ctx);
  3644. memcpy(&p->fl, fl, sizeof(p->fl));
  3645. return p;
  3646. out_free_seqid:
  3647. nfs_free_seqid(p->arg.open_seqid);
  3648. out_free:
  3649. kfree(p);
  3650. return NULL;
  3651. }
  3652. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3653. {
  3654. struct nfs4_lockdata *data = calldata;
  3655. struct nfs4_state *state = data->lsp->ls_state;
  3656. dprintk("%s: begin!\n", __func__);
  3657. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3658. return;
  3659. /* Do we need to do an open_to_lock_owner? */
  3660. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3661. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  3662. return;
  3663. data->arg.open_stateid = &state->stateid;
  3664. data->arg.new_lock_owner = 1;
  3665. data->res.open_seqid = data->arg.open_seqid;
  3666. } else
  3667. data->arg.new_lock_owner = 0;
  3668. data->timestamp = jiffies;
  3669. if (nfs4_setup_sequence(data->server,
  3670. &data->arg.seq_args,
  3671. &data->res.seq_res, 1, task))
  3672. return;
  3673. rpc_call_start(task);
  3674. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  3675. }
  3676. static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
  3677. {
  3678. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  3679. nfs4_lock_prepare(task, calldata);
  3680. }
  3681. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3682. {
  3683. struct nfs4_lockdata *data = calldata;
  3684. dprintk("%s: begin!\n", __func__);
  3685. nfs4_sequence_done(data->server, &data->res.seq_res,
  3686. task->tk_status);
  3687. data->rpc_status = task->tk_status;
  3688. if (RPC_ASSASSINATED(task))
  3689. goto out;
  3690. if (data->arg.new_lock_owner != 0) {
  3691. if (data->rpc_status == 0)
  3692. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3693. else
  3694. goto out;
  3695. }
  3696. if (data->rpc_status == 0) {
  3697. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3698. sizeof(data->lsp->ls_stateid.data));
  3699. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3700. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3701. }
  3702. out:
  3703. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  3704. }
  3705. static void nfs4_lock_release(void *calldata)
  3706. {
  3707. struct nfs4_lockdata *data = calldata;
  3708. dprintk("%s: begin!\n", __func__);
  3709. nfs_free_seqid(data->arg.open_seqid);
  3710. if (data->cancelled != 0) {
  3711. struct rpc_task *task;
  3712. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3713. data->arg.lock_seqid);
  3714. if (!IS_ERR(task))
  3715. rpc_put_task(task);
  3716. dprintk("%s: cancelling lock!\n", __func__);
  3717. } else
  3718. nfs_free_seqid(data->arg.lock_seqid);
  3719. nfs4_put_lock_state(data->lsp);
  3720. put_nfs_open_context(data->ctx);
  3721. kfree(data);
  3722. dprintk("%s: done!\n", __func__);
  3723. }
  3724. static const struct rpc_call_ops nfs4_lock_ops = {
  3725. .rpc_call_prepare = nfs4_lock_prepare,
  3726. .rpc_call_done = nfs4_lock_done,
  3727. .rpc_release = nfs4_lock_release,
  3728. };
  3729. static const struct rpc_call_ops nfs4_recover_lock_ops = {
  3730. .rpc_call_prepare = nfs4_recover_lock_prepare,
  3731. .rpc_call_done = nfs4_lock_done,
  3732. .rpc_release = nfs4_lock_release,
  3733. };
  3734. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  3735. {
  3736. struct nfs_client *clp = server->nfs_client;
  3737. struct nfs4_state *state = lsp->ls_state;
  3738. switch (error) {
  3739. case -NFS4ERR_ADMIN_REVOKED:
  3740. case -NFS4ERR_BAD_STATEID:
  3741. case -NFS4ERR_EXPIRED:
  3742. if (new_lock_owner != 0 ||
  3743. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  3744. nfs4_state_mark_reclaim_nograce(clp, state);
  3745. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  3746. break;
  3747. case -NFS4ERR_STALE_STATEID:
  3748. if (new_lock_owner != 0 ||
  3749. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  3750. nfs4_state_mark_reclaim_reboot(clp, state);
  3751. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  3752. };
  3753. }
  3754. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  3755. {
  3756. struct nfs4_lockdata *data;
  3757. struct rpc_task *task;
  3758. struct rpc_message msg = {
  3759. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3760. .rpc_cred = state->owner->so_cred,
  3761. };
  3762. struct rpc_task_setup task_setup_data = {
  3763. .rpc_client = NFS_CLIENT(state->inode),
  3764. .rpc_message = &msg,
  3765. .callback_ops = &nfs4_lock_ops,
  3766. .workqueue = nfsiod_workqueue,
  3767. .flags = RPC_TASK_ASYNC,
  3768. };
  3769. int ret;
  3770. dprintk("%s: begin!\n", __func__);
  3771. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3772. fl->fl_u.nfs4_fl.owner,
  3773. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  3774. if (data == NULL)
  3775. return -ENOMEM;
  3776. if (IS_SETLKW(cmd))
  3777. data->arg.block = 1;
  3778. if (recovery_type > NFS_LOCK_NEW) {
  3779. if (recovery_type == NFS_LOCK_RECLAIM)
  3780. data->arg.reclaim = NFS_LOCK_RECLAIM;
  3781. task_setup_data.callback_ops = &nfs4_recover_lock_ops;
  3782. }
  3783. msg.rpc_argp = &data->arg,
  3784. msg.rpc_resp = &data->res,
  3785. task_setup_data.callback_data = data;
  3786. task = rpc_run_task(&task_setup_data);
  3787. if (IS_ERR(task))
  3788. return PTR_ERR(task);
  3789. ret = nfs4_wait_for_completion_rpc_task(task);
  3790. if (ret == 0) {
  3791. ret = data->rpc_status;
  3792. if (ret)
  3793. nfs4_handle_setlk_error(data->server, data->lsp,
  3794. data->arg.new_lock_owner, ret);
  3795. } else
  3796. data->cancelled = 1;
  3797. rpc_put_task(task);
  3798. dprintk("%s: done, ret = %d!\n", __func__, ret);
  3799. return ret;
  3800. }
  3801. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3802. {
  3803. struct nfs_server *server = NFS_SERVER(state->inode);
  3804. struct nfs4_exception exception = { };
  3805. int err;
  3806. do {
  3807. /* Cache the lock if possible... */
  3808. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3809. return 0;
  3810. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  3811. if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
  3812. break;
  3813. nfs4_handle_exception(server, err, &exception);
  3814. } while (exception.retry);
  3815. return err;
  3816. }
  3817. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3818. {
  3819. struct nfs_server *server = NFS_SERVER(state->inode);
  3820. struct nfs4_exception exception = { };
  3821. int err;
  3822. err = nfs4_set_lock_state(state, request);
  3823. if (err != 0)
  3824. return err;
  3825. do {
  3826. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3827. return 0;
  3828. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  3829. switch (err) {
  3830. default:
  3831. goto out;
  3832. case -NFS4ERR_GRACE:
  3833. case -NFS4ERR_DELAY:
  3834. case -EKEYEXPIRED:
  3835. nfs4_handle_exception(server, err, &exception);
  3836. err = 0;
  3837. }
  3838. } while (exception.retry);
  3839. out:
  3840. return err;
  3841. }
  3842. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3843. {
  3844. struct nfs_inode *nfsi = NFS_I(state->inode);
  3845. unsigned char fl_flags = request->fl_flags;
  3846. int status = -ENOLCK;
  3847. if ((fl_flags & FL_POSIX) &&
  3848. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  3849. goto out;
  3850. /* Is this a delegated open? */
  3851. status = nfs4_set_lock_state(state, request);
  3852. if (status != 0)
  3853. goto out;
  3854. request->fl_flags |= FL_ACCESS;
  3855. status = do_vfs_lock(request->fl_file, request);
  3856. if (status < 0)
  3857. goto out;
  3858. down_read(&nfsi->rwsem);
  3859. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3860. /* Yes: cache locks! */
  3861. /* ...but avoid races with delegation recall... */
  3862. request->fl_flags = fl_flags & ~FL_SLEEP;
  3863. status = do_vfs_lock(request->fl_file, request);
  3864. goto out_unlock;
  3865. }
  3866. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  3867. if (status != 0)
  3868. goto out_unlock;
  3869. /* Note: we always want to sleep here! */
  3870. request->fl_flags = fl_flags | FL_SLEEP;
  3871. if (do_vfs_lock(request->fl_file, request) < 0)
  3872. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  3873. out_unlock:
  3874. up_read(&nfsi->rwsem);
  3875. out:
  3876. request->fl_flags = fl_flags;
  3877. return status;
  3878. }
  3879. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3880. {
  3881. struct nfs4_exception exception = { };
  3882. int err;
  3883. do {
  3884. err = _nfs4_proc_setlk(state, cmd, request);
  3885. if (err == -NFS4ERR_DENIED)
  3886. err = -EAGAIN;
  3887. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3888. err, &exception);
  3889. } while (exception.retry);
  3890. return err;
  3891. }
  3892. static int
  3893. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3894. {
  3895. struct nfs_open_context *ctx;
  3896. struct nfs4_state *state;
  3897. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3898. int status;
  3899. /* verify open state */
  3900. ctx = nfs_file_open_context(filp);
  3901. state = ctx->state;
  3902. if (request->fl_start < 0 || request->fl_end < 0)
  3903. return -EINVAL;
  3904. if (IS_GETLK(cmd)) {
  3905. if (state != NULL)
  3906. return nfs4_proc_getlk(state, F_GETLK, request);
  3907. return 0;
  3908. }
  3909. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3910. return -EINVAL;
  3911. if (request->fl_type == F_UNLCK) {
  3912. if (state != NULL)
  3913. return nfs4_proc_unlck(state, cmd, request);
  3914. return 0;
  3915. }
  3916. if (state == NULL)
  3917. return -ENOLCK;
  3918. do {
  3919. status = nfs4_proc_setlk(state, cmd, request);
  3920. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3921. break;
  3922. timeout = nfs4_set_lock_task_retry(timeout);
  3923. status = -ERESTARTSYS;
  3924. if (signalled())
  3925. break;
  3926. } while(status < 0);
  3927. return status;
  3928. }
  3929. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3930. {
  3931. struct nfs_server *server = NFS_SERVER(state->inode);
  3932. struct nfs4_exception exception = { };
  3933. int err;
  3934. err = nfs4_set_lock_state(state, fl);
  3935. if (err != 0)
  3936. goto out;
  3937. do {
  3938. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  3939. switch (err) {
  3940. default:
  3941. printk(KERN_ERR "%s: unhandled error %d.\n",
  3942. __func__, err);
  3943. case 0:
  3944. case -ESTALE:
  3945. goto out;
  3946. case -NFS4ERR_EXPIRED:
  3947. case -NFS4ERR_STALE_CLIENTID:
  3948. case -NFS4ERR_STALE_STATEID:
  3949. case -NFS4ERR_BADSESSION:
  3950. case -NFS4ERR_BADSLOT:
  3951. case -NFS4ERR_BAD_HIGH_SLOT:
  3952. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3953. case -NFS4ERR_DEADSESSION:
  3954. nfs4_schedule_state_recovery(server->nfs_client);
  3955. goto out;
  3956. case -ERESTARTSYS:
  3957. /*
  3958. * The show must go on: exit, but mark the
  3959. * stateid as needing recovery.
  3960. */
  3961. case -NFS4ERR_ADMIN_REVOKED:
  3962. case -NFS4ERR_BAD_STATEID:
  3963. case -NFS4ERR_OPENMODE:
  3964. nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
  3965. err = 0;
  3966. goto out;
  3967. case -ENOMEM:
  3968. case -NFS4ERR_DENIED:
  3969. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  3970. err = 0;
  3971. goto out;
  3972. case -NFS4ERR_DELAY:
  3973. case -EKEYEXPIRED:
  3974. break;
  3975. }
  3976. err = nfs4_handle_exception(server, err, &exception);
  3977. } while (exception.retry);
  3978. out:
  3979. return err;
  3980. }
  3981. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3982. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3983. size_t buflen, int flags)
  3984. {
  3985. struct inode *inode = dentry->d_inode;
  3986. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3987. return -EOPNOTSUPP;
  3988. return nfs4_proc_set_acl(inode, buf, buflen);
  3989. }
  3990. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3991. * and that's what we'll do for e.g. user attributes that haven't been set.
  3992. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3993. * attributes in kernel-managed attribute namespaces. */
  3994. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3995. size_t buflen)
  3996. {
  3997. struct inode *inode = dentry->d_inode;
  3998. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3999. return -EOPNOTSUPP;
  4000. return nfs4_proc_get_acl(inode, buf, buflen);
  4001. }
  4002. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  4003. {
  4004. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  4005. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  4006. return 0;
  4007. if (buf && buflen < len)
  4008. return -ERANGE;
  4009. if (buf)
  4010. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  4011. return len;
  4012. }
  4013. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  4014. {
  4015. if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
  4016. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  4017. (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
  4018. return;
  4019. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4020. NFS_ATTR_FATTR_NLINK;
  4021. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4022. fattr->nlink = 2;
  4023. }
  4024. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  4025. struct nfs4_fs_locations *fs_locations, struct page *page)
  4026. {
  4027. struct nfs_server *server = NFS_SERVER(dir);
  4028. u32 bitmask[2] = {
  4029. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4030. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  4031. };
  4032. struct nfs4_fs_locations_arg args = {
  4033. .dir_fh = NFS_FH(dir),
  4034. .name = name,
  4035. .page = page,
  4036. .bitmask = bitmask,
  4037. };
  4038. struct nfs4_fs_locations_res res = {
  4039. .fs_locations = fs_locations,
  4040. };
  4041. struct rpc_message msg = {
  4042. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4043. .rpc_argp = &args,
  4044. .rpc_resp = &res,
  4045. };
  4046. int status;
  4047. dprintk("%s: start\n", __func__);
  4048. nfs_fattr_init(&fs_locations->fattr);
  4049. fs_locations->server = server;
  4050. fs_locations->nlocations = 0;
  4051. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  4052. nfs_fixup_referral_attributes(&fs_locations->fattr);
  4053. dprintk("%s: returned status = %d\n", __func__, status);
  4054. return status;
  4055. }
  4056. #ifdef CONFIG_NFS_V4_1
  4057. /*
  4058. * nfs4_proc_exchange_id()
  4059. *
  4060. * Since the clientid has expired, all compounds using sessions
  4061. * associated with the stale clientid will be returning
  4062. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4063. * be in some phase of session reset.
  4064. */
  4065. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4066. {
  4067. nfs4_verifier verifier;
  4068. struct nfs41_exchange_id_args args = {
  4069. .client = clp,
  4070. .flags = clp->cl_exchange_flags,
  4071. };
  4072. struct nfs41_exchange_id_res res = {
  4073. .client = clp,
  4074. };
  4075. int status;
  4076. struct rpc_message msg = {
  4077. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4078. .rpc_argp = &args,
  4079. .rpc_resp = &res,
  4080. .rpc_cred = cred,
  4081. };
  4082. __be32 *p;
  4083. dprintk("--> %s\n", __func__);
  4084. BUG_ON(clp == NULL);
  4085. /* Remove server-only flags */
  4086. args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
  4087. p = (u32 *)verifier.data;
  4088. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  4089. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  4090. args.verifier = &verifier;
  4091. while (1) {
  4092. args.id_len = scnprintf(args.id, sizeof(args.id),
  4093. "%s/%s %u",
  4094. clp->cl_ipaddr,
  4095. rpc_peeraddr2str(clp->cl_rpcclient,
  4096. RPC_DISPLAY_ADDR),
  4097. clp->cl_id_uniquifier);
  4098. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  4099. if (status != -NFS4ERR_CLID_INUSE)
  4100. break;
  4101. if (signalled())
  4102. break;
  4103. if (++clp->cl_id_uniquifier == 0)
  4104. break;
  4105. }
  4106. dprintk("<-- %s status= %d\n", __func__, status);
  4107. return status;
  4108. }
  4109. struct nfs4_get_lease_time_data {
  4110. struct nfs4_get_lease_time_args *args;
  4111. struct nfs4_get_lease_time_res *res;
  4112. struct nfs_client *clp;
  4113. };
  4114. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  4115. void *calldata)
  4116. {
  4117. int ret;
  4118. struct nfs4_get_lease_time_data *data =
  4119. (struct nfs4_get_lease_time_data *)calldata;
  4120. dprintk("--> %s\n", __func__);
  4121. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4122. /* just setup sequence, do not trigger session recovery
  4123. since we're invoked within one */
  4124. ret = nfs41_setup_sequence(data->clp->cl_session,
  4125. &data->args->la_seq_args,
  4126. &data->res->lr_seq_res, 0, task);
  4127. BUG_ON(ret == -EAGAIN);
  4128. rpc_call_start(task);
  4129. dprintk("<-- %s\n", __func__);
  4130. }
  4131. /*
  4132. * Called from nfs4_state_manager thread for session setup, so don't recover
  4133. * from sequence operation or clientid errors.
  4134. */
  4135. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  4136. {
  4137. struct nfs4_get_lease_time_data *data =
  4138. (struct nfs4_get_lease_time_data *)calldata;
  4139. dprintk("--> %s\n", __func__);
  4140. nfs41_sequence_done(&data->res->lr_seq_res);
  4141. switch (task->tk_status) {
  4142. case -NFS4ERR_DELAY:
  4143. case -NFS4ERR_GRACE:
  4144. case -EKEYEXPIRED:
  4145. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  4146. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  4147. task->tk_status = 0;
  4148. nfs_restart_rpc(task, data->clp);
  4149. return;
  4150. }
  4151. dprintk("<-- %s\n", __func__);
  4152. }
  4153. struct rpc_call_ops nfs4_get_lease_time_ops = {
  4154. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  4155. .rpc_call_done = nfs4_get_lease_time_done,
  4156. };
  4157. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  4158. {
  4159. struct rpc_task *task;
  4160. struct nfs4_get_lease_time_args args;
  4161. struct nfs4_get_lease_time_res res = {
  4162. .lr_fsinfo = fsinfo,
  4163. };
  4164. struct nfs4_get_lease_time_data data = {
  4165. .args = &args,
  4166. .res = &res,
  4167. .clp = clp,
  4168. };
  4169. struct rpc_message msg = {
  4170. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  4171. .rpc_argp = &args,
  4172. .rpc_resp = &res,
  4173. };
  4174. struct rpc_task_setup task_setup = {
  4175. .rpc_client = clp->cl_rpcclient,
  4176. .rpc_message = &msg,
  4177. .callback_ops = &nfs4_get_lease_time_ops,
  4178. .callback_data = &data
  4179. };
  4180. int status;
  4181. res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4182. dprintk("--> %s\n", __func__);
  4183. task = rpc_run_task(&task_setup);
  4184. if (IS_ERR(task))
  4185. status = PTR_ERR(task);
  4186. else {
  4187. status = task->tk_status;
  4188. rpc_put_task(task);
  4189. }
  4190. dprintk("<-- %s return %d\n", __func__, status);
  4191. return status;
  4192. }
  4193. /*
  4194. * Reset a slot table
  4195. */
  4196. static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
  4197. int ivalue)
  4198. {
  4199. struct nfs4_slot *new = NULL;
  4200. int i;
  4201. int ret = 0;
  4202. dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
  4203. max_reqs, tbl->max_slots);
  4204. /* Does the newly negotiated max_reqs match the existing slot table? */
  4205. if (max_reqs != tbl->max_slots) {
  4206. ret = -ENOMEM;
  4207. new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
  4208. GFP_NOFS);
  4209. if (!new)
  4210. goto out;
  4211. ret = 0;
  4212. kfree(tbl->slots);
  4213. }
  4214. spin_lock(&tbl->slot_tbl_lock);
  4215. if (new) {
  4216. tbl->slots = new;
  4217. tbl->max_slots = max_reqs;
  4218. }
  4219. for (i = 0; i < tbl->max_slots; ++i)
  4220. tbl->slots[i].seq_nr = ivalue;
  4221. spin_unlock(&tbl->slot_tbl_lock);
  4222. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4223. tbl, tbl->slots, tbl->max_slots);
  4224. out:
  4225. dprintk("<-- %s: return %d\n", __func__, ret);
  4226. return ret;
  4227. }
  4228. /*
  4229. * Reset the forechannel and backchannel slot tables
  4230. */
  4231. static int nfs4_reset_slot_tables(struct nfs4_session *session)
  4232. {
  4233. int status;
  4234. status = nfs4_reset_slot_table(&session->fc_slot_table,
  4235. session->fc_attrs.max_reqs, 1);
  4236. if (status)
  4237. return status;
  4238. status = nfs4_reset_slot_table(&session->bc_slot_table,
  4239. session->bc_attrs.max_reqs, 0);
  4240. return status;
  4241. }
  4242. /* Destroy the slot table */
  4243. static void nfs4_destroy_slot_tables(struct nfs4_session *session)
  4244. {
  4245. if (session->fc_slot_table.slots != NULL) {
  4246. kfree(session->fc_slot_table.slots);
  4247. session->fc_slot_table.slots = NULL;
  4248. }
  4249. if (session->bc_slot_table.slots != NULL) {
  4250. kfree(session->bc_slot_table.slots);
  4251. session->bc_slot_table.slots = NULL;
  4252. }
  4253. return;
  4254. }
  4255. /*
  4256. * Initialize slot table
  4257. */
  4258. static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
  4259. int max_slots, int ivalue)
  4260. {
  4261. struct nfs4_slot *slot;
  4262. int ret = -ENOMEM;
  4263. BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
  4264. dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
  4265. slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
  4266. if (!slot)
  4267. goto out;
  4268. ret = 0;
  4269. spin_lock(&tbl->slot_tbl_lock);
  4270. tbl->max_slots = max_slots;
  4271. tbl->slots = slot;
  4272. tbl->highest_used_slotid = -1; /* no slot is currently used */
  4273. spin_unlock(&tbl->slot_tbl_lock);
  4274. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4275. tbl, tbl->slots, tbl->max_slots);
  4276. out:
  4277. dprintk("<-- %s: return %d\n", __func__, ret);
  4278. return ret;
  4279. }
  4280. /*
  4281. * Initialize the forechannel and backchannel tables
  4282. */
  4283. static int nfs4_init_slot_tables(struct nfs4_session *session)
  4284. {
  4285. struct nfs4_slot_table *tbl;
  4286. int status = 0;
  4287. tbl = &session->fc_slot_table;
  4288. if (tbl->slots == NULL) {
  4289. status = nfs4_init_slot_table(tbl,
  4290. session->fc_attrs.max_reqs, 1);
  4291. if (status)
  4292. return status;
  4293. }
  4294. tbl = &session->bc_slot_table;
  4295. if (tbl->slots == NULL) {
  4296. status = nfs4_init_slot_table(tbl,
  4297. session->bc_attrs.max_reqs, 0);
  4298. if (status)
  4299. nfs4_destroy_slot_tables(session);
  4300. }
  4301. return status;
  4302. }
  4303. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  4304. {
  4305. struct nfs4_session *session;
  4306. struct nfs4_slot_table *tbl;
  4307. session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  4308. if (!session)
  4309. return NULL;
  4310. init_completion(&session->complete);
  4311. tbl = &session->fc_slot_table;
  4312. tbl->highest_used_slotid = -1;
  4313. spin_lock_init(&tbl->slot_tbl_lock);
  4314. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  4315. tbl = &session->bc_slot_table;
  4316. tbl->highest_used_slotid = -1;
  4317. spin_lock_init(&tbl->slot_tbl_lock);
  4318. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  4319. session->clp = clp;
  4320. return session;
  4321. }
  4322. void nfs4_destroy_session(struct nfs4_session *session)
  4323. {
  4324. nfs4_proc_destroy_session(session);
  4325. dprintk("%s Destroy backchannel for xprt %p\n",
  4326. __func__, session->clp->cl_rpcclient->cl_xprt);
  4327. xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
  4328. NFS41_BC_MIN_CALLBACKS);
  4329. nfs4_destroy_slot_tables(session);
  4330. kfree(session);
  4331. }
  4332. /*
  4333. * Initialize the values to be used by the client in CREATE_SESSION
  4334. * If nfs4_init_session set the fore channel request and response sizes,
  4335. * use them.
  4336. *
  4337. * Set the back channel max_resp_sz_cached to zero to force the client to
  4338. * always set csa_cachethis to FALSE because the current implementation
  4339. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  4340. */
  4341. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  4342. {
  4343. struct nfs4_session *session = args->client->cl_session;
  4344. unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
  4345. mxresp_sz = session->fc_attrs.max_resp_sz;
  4346. if (mxrqst_sz == 0)
  4347. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  4348. if (mxresp_sz == 0)
  4349. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  4350. /* Fore channel attributes */
  4351. args->fc_attrs.headerpadsz = 0;
  4352. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  4353. args->fc_attrs.max_resp_sz = mxresp_sz;
  4354. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  4355. args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
  4356. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  4357. "max_ops=%u max_reqs=%u\n",
  4358. __func__,
  4359. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  4360. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  4361. /* Back channel attributes */
  4362. args->bc_attrs.headerpadsz = 0;
  4363. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  4364. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  4365. args->bc_attrs.max_resp_sz_cached = 0;
  4366. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  4367. args->bc_attrs.max_reqs = 1;
  4368. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  4369. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4370. __func__,
  4371. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  4372. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  4373. args->bc_attrs.max_reqs);
  4374. }
  4375. static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
  4376. {
  4377. if (rcvd <= sent)
  4378. return 0;
  4379. printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
  4380. "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
  4381. return -EINVAL;
  4382. }
  4383. #define _verify_fore_channel_attr(_name_) \
  4384. _verify_channel_attr("fore", #_name_, \
  4385. args->fc_attrs._name_, \
  4386. session->fc_attrs._name_)
  4387. #define _verify_back_channel_attr(_name_) \
  4388. _verify_channel_attr("back", #_name_, \
  4389. args->bc_attrs._name_, \
  4390. session->bc_attrs._name_)
  4391. /*
  4392. * The server is not allowed to increase the fore channel header pad size,
  4393. * maximum response size, or maximum number of operations.
  4394. *
  4395. * The back channel attributes are only negotiatied down: We send what the
  4396. * (back channel) server insists upon.
  4397. */
  4398. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  4399. struct nfs4_session *session)
  4400. {
  4401. int ret = 0;
  4402. ret |= _verify_fore_channel_attr(headerpadsz);
  4403. ret |= _verify_fore_channel_attr(max_resp_sz);
  4404. ret |= _verify_fore_channel_attr(max_ops);
  4405. ret |= _verify_back_channel_attr(headerpadsz);
  4406. ret |= _verify_back_channel_attr(max_rqst_sz);
  4407. ret |= _verify_back_channel_attr(max_resp_sz);
  4408. ret |= _verify_back_channel_attr(max_resp_sz_cached);
  4409. ret |= _verify_back_channel_attr(max_ops);
  4410. ret |= _verify_back_channel_attr(max_reqs);
  4411. return ret;
  4412. }
  4413. static int _nfs4_proc_create_session(struct nfs_client *clp)
  4414. {
  4415. struct nfs4_session *session = clp->cl_session;
  4416. struct nfs41_create_session_args args = {
  4417. .client = clp,
  4418. .cb_program = NFS4_CALLBACK,
  4419. };
  4420. struct nfs41_create_session_res res = {
  4421. .client = clp,
  4422. };
  4423. struct rpc_message msg = {
  4424. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  4425. .rpc_argp = &args,
  4426. .rpc_resp = &res,
  4427. };
  4428. int status;
  4429. nfs4_init_channel_attrs(&args);
  4430. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  4431. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4432. if (!status)
  4433. /* Verify the session's negotiated channel_attrs values */
  4434. status = nfs4_verify_channel_attrs(&args, session);
  4435. if (!status) {
  4436. /* Increment the clientid slot sequence id */
  4437. clp->cl_seqid++;
  4438. }
  4439. return status;
  4440. }
  4441. /*
  4442. * Issues a CREATE_SESSION operation to the server.
  4443. * It is the responsibility of the caller to verify the session is
  4444. * expired before calling this routine.
  4445. */
  4446. int nfs4_proc_create_session(struct nfs_client *clp)
  4447. {
  4448. int status;
  4449. unsigned *ptr;
  4450. struct nfs4_session *session = clp->cl_session;
  4451. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  4452. status = _nfs4_proc_create_session(clp);
  4453. if (status)
  4454. goto out;
  4455. /* Init and reset the fore channel */
  4456. status = nfs4_init_slot_tables(session);
  4457. dprintk("slot table initialization returned %d\n", status);
  4458. if (status)
  4459. goto out;
  4460. status = nfs4_reset_slot_tables(session);
  4461. dprintk("slot table reset returned %d\n", status);
  4462. if (status)
  4463. goto out;
  4464. ptr = (unsigned *)&session->sess_id.data[0];
  4465. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  4466. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  4467. out:
  4468. dprintk("<-- %s\n", __func__);
  4469. return status;
  4470. }
  4471. /*
  4472. * Issue the over-the-wire RPC DESTROY_SESSION.
  4473. * The caller must serialize access to this routine.
  4474. */
  4475. int nfs4_proc_destroy_session(struct nfs4_session *session)
  4476. {
  4477. int status = 0;
  4478. struct rpc_message msg;
  4479. dprintk("--> nfs4_proc_destroy_session\n");
  4480. /* session is still being setup */
  4481. if (session->clp->cl_cons_state != NFS_CS_READY)
  4482. return status;
  4483. msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
  4484. msg.rpc_argp = session;
  4485. msg.rpc_resp = NULL;
  4486. msg.rpc_cred = NULL;
  4487. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4488. if (status)
  4489. printk(KERN_WARNING
  4490. "Got error %d from the server on DESTROY_SESSION. "
  4491. "Session has been destroyed regardless...\n", status);
  4492. dprintk("<-- nfs4_proc_destroy_session\n");
  4493. return status;
  4494. }
  4495. int nfs4_init_session(struct nfs_server *server)
  4496. {
  4497. struct nfs_client *clp = server->nfs_client;
  4498. struct nfs4_session *session;
  4499. unsigned int rsize, wsize;
  4500. int ret;
  4501. if (!nfs4_has_session(clp))
  4502. return 0;
  4503. rsize = server->rsize;
  4504. if (rsize == 0)
  4505. rsize = NFS_MAX_FILE_IO_SIZE;
  4506. wsize = server->wsize;
  4507. if (wsize == 0)
  4508. wsize = NFS_MAX_FILE_IO_SIZE;
  4509. session = clp->cl_session;
  4510. session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
  4511. session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
  4512. ret = nfs4_recover_expired_lease(server);
  4513. if (!ret)
  4514. ret = nfs4_check_client_ready(clp);
  4515. return ret;
  4516. }
  4517. /*
  4518. * Renew the cl_session lease.
  4519. */
  4520. struct nfs4_sequence_data {
  4521. struct nfs_client *clp;
  4522. struct nfs4_sequence_args args;
  4523. struct nfs4_sequence_res res;
  4524. };
  4525. static void nfs41_sequence_release(void *data)
  4526. {
  4527. struct nfs4_sequence_data *calldata = data;
  4528. struct nfs_client *clp = calldata->clp;
  4529. if (atomic_read(&clp->cl_count) > 1)
  4530. nfs4_schedule_state_renewal(clp);
  4531. nfs_put_client(clp);
  4532. kfree(calldata);
  4533. }
  4534. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  4535. {
  4536. switch(task->tk_status) {
  4537. case -NFS4ERR_DELAY:
  4538. case -EKEYEXPIRED:
  4539. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  4540. return -EAGAIN;
  4541. default:
  4542. nfs4_schedule_state_recovery(clp);
  4543. }
  4544. return 0;
  4545. }
  4546. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  4547. {
  4548. struct nfs4_sequence_data *calldata = data;
  4549. struct nfs_client *clp = calldata->clp;
  4550. nfs41_sequence_done(task->tk_msg.rpc_resp);
  4551. if (task->tk_status < 0) {
  4552. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  4553. if (atomic_read(&clp->cl_count) == 1)
  4554. goto out;
  4555. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  4556. rpc_restart_call_prepare(task);
  4557. return;
  4558. }
  4559. }
  4560. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  4561. out:
  4562. dprintk("<-- %s\n", __func__);
  4563. }
  4564. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  4565. {
  4566. struct nfs4_sequence_data *calldata = data;
  4567. struct nfs_client *clp = calldata->clp;
  4568. struct nfs4_sequence_args *args;
  4569. struct nfs4_sequence_res *res;
  4570. args = task->tk_msg.rpc_argp;
  4571. res = task->tk_msg.rpc_resp;
  4572. if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
  4573. return;
  4574. rpc_call_start(task);
  4575. }
  4576. static const struct rpc_call_ops nfs41_sequence_ops = {
  4577. .rpc_call_done = nfs41_sequence_call_done,
  4578. .rpc_call_prepare = nfs41_sequence_prepare,
  4579. .rpc_release = nfs41_sequence_release,
  4580. };
  4581. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  4582. {
  4583. struct nfs4_sequence_data *calldata;
  4584. struct rpc_message msg = {
  4585. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4586. .rpc_cred = cred,
  4587. };
  4588. struct rpc_task_setup task_setup_data = {
  4589. .rpc_client = clp->cl_rpcclient,
  4590. .rpc_message = &msg,
  4591. .callback_ops = &nfs41_sequence_ops,
  4592. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
  4593. };
  4594. if (!atomic_inc_not_zero(&clp->cl_count))
  4595. return ERR_PTR(-EIO);
  4596. calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
  4597. if (calldata == NULL) {
  4598. nfs_put_client(clp);
  4599. return ERR_PTR(-ENOMEM);
  4600. }
  4601. calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4602. msg.rpc_argp = &calldata->args;
  4603. msg.rpc_resp = &calldata->res;
  4604. calldata->clp = clp;
  4605. task_setup_data.callback_data = calldata;
  4606. return rpc_run_task(&task_setup_data);
  4607. }
  4608. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  4609. {
  4610. struct rpc_task *task;
  4611. int ret = 0;
  4612. task = _nfs41_proc_sequence(clp, cred);
  4613. if (IS_ERR(task))
  4614. ret = PTR_ERR(task);
  4615. else
  4616. rpc_put_task(task);
  4617. dprintk("<-- %s status=%d\n", __func__, ret);
  4618. return ret;
  4619. }
  4620. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  4621. {
  4622. struct rpc_task *task;
  4623. int ret;
  4624. task = _nfs41_proc_sequence(clp, cred);
  4625. if (IS_ERR(task)) {
  4626. ret = PTR_ERR(task);
  4627. goto out;
  4628. }
  4629. ret = rpc_wait_for_completion_task(task);
  4630. if (!ret)
  4631. ret = task->tk_status;
  4632. rpc_put_task(task);
  4633. out:
  4634. dprintk("<-- %s status=%d\n", __func__, ret);
  4635. return ret;
  4636. }
  4637. struct nfs4_reclaim_complete_data {
  4638. struct nfs_client *clp;
  4639. struct nfs41_reclaim_complete_args arg;
  4640. struct nfs41_reclaim_complete_res res;
  4641. };
  4642. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  4643. {
  4644. struct nfs4_reclaim_complete_data *calldata = data;
  4645. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4646. if (nfs41_setup_sequence(calldata->clp->cl_session,
  4647. &calldata->arg.seq_args,
  4648. &calldata->res.seq_res, 0, task))
  4649. return;
  4650. rpc_call_start(task);
  4651. }
  4652. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  4653. {
  4654. switch(task->tk_status) {
  4655. case 0:
  4656. case -NFS4ERR_COMPLETE_ALREADY:
  4657. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  4658. break;
  4659. case -NFS4ERR_DELAY:
  4660. case -EKEYEXPIRED:
  4661. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  4662. return -EAGAIN;
  4663. default:
  4664. nfs4_schedule_state_recovery(clp);
  4665. }
  4666. return 0;
  4667. }
  4668. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  4669. {
  4670. struct nfs4_reclaim_complete_data *calldata = data;
  4671. struct nfs_client *clp = calldata->clp;
  4672. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  4673. dprintk("--> %s\n", __func__);
  4674. nfs41_sequence_done(res);
  4675. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  4676. rpc_restart_call_prepare(task);
  4677. return;
  4678. }
  4679. dprintk("<-- %s\n", __func__);
  4680. }
  4681. static void nfs4_free_reclaim_complete_data(void *data)
  4682. {
  4683. struct nfs4_reclaim_complete_data *calldata = data;
  4684. kfree(calldata);
  4685. }
  4686. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  4687. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  4688. .rpc_call_done = nfs4_reclaim_complete_done,
  4689. .rpc_release = nfs4_free_reclaim_complete_data,
  4690. };
  4691. /*
  4692. * Issue a global reclaim complete.
  4693. */
  4694. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  4695. {
  4696. struct nfs4_reclaim_complete_data *calldata;
  4697. struct rpc_task *task;
  4698. struct rpc_message msg = {
  4699. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  4700. };
  4701. struct rpc_task_setup task_setup_data = {
  4702. .rpc_client = clp->cl_rpcclient,
  4703. .rpc_message = &msg,
  4704. .callback_ops = &nfs4_reclaim_complete_call_ops,
  4705. .flags = RPC_TASK_ASYNC,
  4706. };
  4707. int status = -ENOMEM;
  4708. dprintk("--> %s\n", __func__);
  4709. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  4710. if (calldata == NULL)
  4711. goto out;
  4712. calldata->clp = clp;
  4713. calldata->arg.one_fs = 0;
  4714. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4715. msg.rpc_argp = &calldata->arg;
  4716. msg.rpc_resp = &calldata->res;
  4717. task_setup_data.callback_data = calldata;
  4718. task = rpc_run_task(&task_setup_data);
  4719. if (IS_ERR(task)) {
  4720. status = PTR_ERR(task);
  4721. goto out;
  4722. }
  4723. rpc_put_task(task);
  4724. return 0;
  4725. out:
  4726. dprintk("<-- %s status=%d\n", __func__, status);
  4727. return status;
  4728. }
  4729. #endif /* CONFIG_NFS_V4_1 */
  4730. struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  4731. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4732. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4733. .recover_open = nfs4_open_reclaim,
  4734. .recover_lock = nfs4_lock_reclaim,
  4735. .establish_clid = nfs4_init_clientid,
  4736. .get_clid_cred = nfs4_get_setclientid_cred,
  4737. };
  4738. #if defined(CONFIG_NFS_V4_1)
  4739. struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  4740. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4741. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4742. .recover_open = nfs4_open_reclaim,
  4743. .recover_lock = nfs4_lock_reclaim,
  4744. .establish_clid = nfs41_init_clientid,
  4745. .get_clid_cred = nfs4_get_exchange_id_cred,
  4746. .reclaim_complete = nfs41_proc_reclaim_complete,
  4747. };
  4748. #endif /* CONFIG_NFS_V4_1 */
  4749. struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  4750. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4751. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4752. .recover_open = nfs4_open_expired,
  4753. .recover_lock = nfs4_lock_expired,
  4754. .establish_clid = nfs4_init_clientid,
  4755. .get_clid_cred = nfs4_get_setclientid_cred,
  4756. };
  4757. #if defined(CONFIG_NFS_V4_1)
  4758. struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  4759. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4760. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4761. .recover_open = nfs4_open_expired,
  4762. .recover_lock = nfs4_lock_expired,
  4763. .establish_clid = nfs41_init_clientid,
  4764. .get_clid_cred = nfs4_get_exchange_id_cred,
  4765. };
  4766. #endif /* CONFIG_NFS_V4_1 */
  4767. struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  4768. .sched_state_renewal = nfs4_proc_async_renew,
  4769. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  4770. .renew_lease = nfs4_proc_renew,
  4771. };
  4772. #if defined(CONFIG_NFS_V4_1)
  4773. struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  4774. .sched_state_renewal = nfs41_proc_async_sequence,
  4775. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  4776. .renew_lease = nfs4_proc_sequence,
  4777. };
  4778. #endif
  4779. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  4780. .minor_version = 0,
  4781. .call_sync = _nfs4_call_sync,
  4782. .validate_stateid = nfs4_validate_delegation_stateid,
  4783. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  4784. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  4785. .state_renewal_ops = &nfs40_state_renewal_ops,
  4786. };
  4787. #if defined(CONFIG_NFS_V4_1)
  4788. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  4789. .minor_version = 1,
  4790. .call_sync = _nfs4_call_sync_session,
  4791. .validate_stateid = nfs41_validate_delegation_stateid,
  4792. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  4793. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  4794. .state_renewal_ops = &nfs41_state_renewal_ops,
  4795. };
  4796. #endif
  4797. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  4798. [0] = &nfs_v4_0_minor_ops,
  4799. #if defined(CONFIG_NFS_V4_1)
  4800. [1] = &nfs_v4_1_minor_ops,
  4801. #endif
  4802. };
  4803. static const struct inode_operations nfs4_file_inode_operations = {
  4804. .permission = nfs_permission,
  4805. .getattr = nfs_getattr,
  4806. .setattr = nfs_setattr,
  4807. .getxattr = nfs4_getxattr,
  4808. .setxattr = nfs4_setxattr,
  4809. .listxattr = nfs4_listxattr,
  4810. };
  4811. const struct nfs_rpc_ops nfs_v4_clientops = {
  4812. .version = 4, /* protocol version */
  4813. .dentry_ops = &nfs4_dentry_operations,
  4814. .dir_inode_ops = &nfs4_dir_inode_operations,
  4815. .file_inode_ops = &nfs4_file_inode_operations,
  4816. .getroot = nfs4_proc_get_root,
  4817. .getattr = nfs4_proc_getattr,
  4818. .setattr = nfs4_proc_setattr,
  4819. .lookupfh = nfs4_proc_lookupfh,
  4820. .lookup = nfs4_proc_lookup,
  4821. .access = nfs4_proc_access,
  4822. .readlink = nfs4_proc_readlink,
  4823. .create = nfs4_proc_create,
  4824. .remove = nfs4_proc_remove,
  4825. .unlink_setup = nfs4_proc_unlink_setup,
  4826. .unlink_done = nfs4_proc_unlink_done,
  4827. .rename = nfs4_proc_rename,
  4828. .link = nfs4_proc_link,
  4829. .symlink = nfs4_proc_symlink,
  4830. .mkdir = nfs4_proc_mkdir,
  4831. .rmdir = nfs4_proc_remove,
  4832. .readdir = nfs4_proc_readdir,
  4833. .mknod = nfs4_proc_mknod,
  4834. .statfs = nfs4_proc_statfs,
  4835. .fsinfo = nfs4_proc_fsinfo,
  4836. .pathconf = nfs4_proc_pathconf,
  4837. .set_capabilities = nfs4_server_capabilities,
  4838. .decode_dirent = nfs4_decode_dirent,
  4839. .read_setup = nfs4_proc_read_setup,
  4840. .read_done = nfs4_read_done,
  4841. .write_setup = nfs4_proc_write_setup,
  4842. .write_done = nfs4_write_done,
  4843. .commit_setup = nfs4_proc_commit_setup,
  4844. .commit_done = nfs4_commit_done,
  4845. .lock = nfs4_proc_lock,
  4846. .clear_acl_cache = nfs4_zap_acl_attr,
  4847. .close_context = nfs4_close_context,
  4848. };
  4849. /*
  4850. * Local variables:
  4851. * c-basic-offset: 8
  4852. * End:
  4853. */