nfs4proc.c 144 KB

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