nfs4proc.c 138 KB

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