nfs4proc.c 143 KB

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