nfs4proc.c 141 KB

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