nfs4proc.c 136 KB

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