nfs4proc.c 143 KB

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