nfs4proc.c 136 KB

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