nfs4proc.c 136 KB

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