nfs4state.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include "xdr4.h"
  44. #include "vfs.h"
  45. #include "current_stateid.h"
  46. #include "fault_inject.h"
  47. #include "netns.h"
  48. #define NFSDDBG_FACILITY NFSDDBG_PROC
  49. /* Globals */
  50. time_t nfsd4_grace = 90;
  51. #define all_ones {{~0,~0},~0}
  52. static const stateid_t one_stateid = {
  53. .si_generation = ~0,
  54. .si_opaque = all_ones,
  55. };
  56. static const stateid_t zero_stateid = {
  57. /* all fields zero */
  58. };
  59. static const stateid_t currentstateid = {
  60. .si_generation = 1,
  61. };
  62. static u64 current_sessionid = 1;
  63. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  64. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  65. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  66. /* forward declarations */
  67. static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
  68. /* Locking: */
  69. /* Currently used for almost all code touching nfsv4 state: */
  70. static DEFINE_MUTEX(client_mutex);
  71. /*
  72. * Currently used for the del_recall_lru and file hash table. In an
  73. * effort to decrease the scope of the client_mutex, this spinlock may
  74. * eventually cover more:
  75. */
  76. static DEFINE_SPINLOCK(recall_lock);
  77. static struct kmem_cache *openowner_slab = NULL;
  78. static struct kmem_cache *lockowner_slab = NULL;
  79. static struct kmem_cache *file_slab = NULL;
  80. static struct kmem_cache *stateid_slab = NULL;
  81. static struct kmem_cache *deleg_slab = NULL;
  82. void
  83. nfs4_lock_state(void)
  84. {
  85. mutex_lock(&client_mutex);
  86. }
  87. static void free_session(struct kref *);
  88. /* Must be called under the client_lock */
  89. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  90. {
  91. kref_put(&ses->se_ref, free_session);
  92. }
  93. static void nfsd4_get_session(struct nfsd4_session *ses)
  94. {
  95. kref_get(&ses->se_ref);
  96. }
  97. void
  98. nfs4_unlock_state(void)
  99. {
  100. mutex_unlock(&client_mutex);
  101. }
  102. static inline u32
  103. opaque_hashval(const void *ptr, int nbytes)
  104. {
  105. unsigned char *cptr = (unsigned char *) ptr;
  106. u32 x = 0;
  107. while (nbytes--) {
  108. x *= 37;
  109. x += *cptr++;
  110. }
  111. return x;
  112. }
  113. static struct list_head del_recall_lru;
  114. static void nfsd4_free_file(struct nfs4_file *f)
  115. {
  116. kmem_cache_free(file_slab, f);
  117. }
  118. static inline void
  119. put_nfs4_file(struct nfs4_file *fi)
  120. {
  121. if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
  122. list_del(&fi->fi_hash);
  123. spin_unlock(&recall_lock);
  124. iput(fi->fi_inode);
  125. nfsd4_free_file(fi);
  126. }
  127. }
  128. static inline void
  129. get_nfs4_file(struct nfs4_file *fi)
  130. {
  131. atomic_inc(&fi->fi_ref);
  132. }
  133. static int num_delegations;
  134. unsigned int max_delegations;
  135. /*
  136. * Open owner state (share locks)
  137. */
  138. /* hash tables for lock and open owners */
  139. #define OWNER_HASH_BITS 8
  140. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  141. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  142. static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
  143. {
  144. unsigned int ret;
  145. ret = opaque_hashval(ownername->data, ownername->len);
  146. ret += clientid;
  147. return ret & OWNER_HASH_MASK;
  148. }
  149. /* hash table for nfs4_file */
  150. #define FILE_HASH_BITS 8
  151. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  152. static unsigned int file_hashval(struct inode *ino)
  153. {
  154. /* XXX: why are we hashing on inode pointer, anyway? */
  155. return hash_ptr(ino, FILE_HASH_BITS);
  156. }
  157. static struct list_head file_hashtbl[FILE_HASH_SIZE];
  158. static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  159. {
  160. WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
  161. atomic_inc(&fp->fi_access[oflag]);
  162. }
  163. static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  164. {
  165. if (oflag == O_RDWR) {
  166. __nfs4_file_get_access(fp, O_RDONLY);
  167. __nfs4_file_get_access(fp, O_WRONLY);
  168. } else
  169. __nfs4_file_get_access(fp, oflag);
  170. }
  171. static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
  172. {
  173. if (fp->fi_fds[oflag]) {
  174. fput(fp->fi_fds[oflag]);
  175. fp->fi_fds[oflag] = NULL;
  176. }
  177. }
  178. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  179. {
  180. if (atomic_dec_and_test(&fp->fi_access[oflag])) {
  181. nfs4_file_put_fd(fp, oflag);
  182. /*
  183. * It's also safe to get rid of the RDWR open *if*
  184. * we no longer have need of the other kind of access
  185. * or if we already have the other kind of open:
  186. */
  187. if (fp->fi_fds[1-oflag]
  188. || atomic_read(&fp->fi_access[1 - oflag]) == 0)
  189. nfs4_file_put_fd(fp, O_RDWR);
  190. }
  191. }
  192. static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  193. {
  194. if (oflag == O_RDWR) {
  195. __nfs4_file_put_access(fp, O_RDONLY);
  196. __nfs4_file_put_access(fp, O_WRONLY);
  197. } else
  198. __nfs4_file_put_access(fp, oflag);
  199. }
  200. static inline int get_new_stid(struct nfs4_stid *stid)
  201. {
  202. static int min_stateid = 0;
  203. struct idr *stateids = &stid->sc_client->cl_stateids;
  204. int new_stid;
  205. int error;
  206. error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
  207. /*
  208. * Note: the necessary preallocation was done in
  209. * nfs4_alloc_stateid(). The idr code caps the number of
  210. * preallocations that can exist at a time, but the state lock
  211. * prevents anyone from using ours before we get here:
  212. */
  213. WARN_ON_ONCE(error);
  214. /*
  215. * It shouldn't be a problem to reuse an opaque stateid value.
  216. * I don't think it is for 4.1. But with 4.0 I worry that, for
  217. * example, a stray write retransmission could be accepted by
  218. * the server when it should have been rejected. Therefore,
  219. * adopt a trick from the sctp code to attempt to maximize the
  220. * amount of time until an id is reused, by ensuring they always
  221. * "increase" (mod INT_MAX):
  222. */
  223. min_stateid = new_stid+1;
  224. if (min_stateid == INT_MAX)
  225. min_stateid = 0;
  226. return new_stid;
  227. }
  228. static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
  229. {
  230. stateid_t *s = &stid->sc_stateid;
  231. int new_id;
  232. stid->sc_type = type;
  233. stid->sc_client = cl;
  234. s->si_opaque.so_clid = cl->cl_clientid;
  235. new_id = get_new_stid(stid);
  236. s->si_opaque.so_id = (u32)new_id;
  237. /* Will be incremented before return to client: */
  238. s->si_generation = 0;
  239. }
  240. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
  241. {
  242. struct idr *stateids = &cl->cl_stateids;
  243. if (!idr_pre_get(stateids, GFP_KERNEL))
  244. return NULL;
  245. /*
  246. * Note: if we fail here (or any time between now and the time
  247. * we actually get the new idr), we won't need to undo the idr
  248. * preallocation, since the idr code caps the number of
  249. * preallocated entries.
  250. */
  251. return kmem_cache_alloc(slab, GFP_KERNEL);
  252. }
  253. static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  254. {
  255. return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
  256. }
  257. static struct nfs4_delegation *
  258. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
  259. {
  260. struct nfs4_delegation *dp;
  261. struct nfs4_file *fp = stp->st_file;
  262. dprintk("NFSD alloc_init_deleg\n");
  263. /*
  264. * Major work on the lease subsystem (for example, to support
  265. * calbacks on stat) will be required before we can support
  266. * write delegations properly.
  267. */
  268. if (type != NFS4_OPEN_DELEGATE_READ)
  269. return NULL;
  270. if (fp->fi_had_conflict)
  271. return NULL;
  272. if (num_delegations > max_delegations)
  273. return NULL;
  274. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  275. if (dp == NULL)
  276. return dp;
  277. init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
  278. /*
  279. * delegation seqid's are never incremented. The 4.1 special
  280. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  281. * 0 anyway just for consistency and use 1:
  282. */
  283. dp->dl_stid.sc_stateid.si_generation = 1;
  284. num_delegations++;
  285. INIT_LIST_HEAD(&dp->dl_perfile);
  286. INIT_LIST_HEAD(&dp->dl_perclnt);
  287. INIT_LIST_HEAD(&dp->dl_recall_lru);
  288. get_nfs4_file(fp);
  289. dp->dl_file = fp;
  290. dp->dl_type = type;
  291. fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
  292. dp->dl_time = 0;
  293. atomic_set(&dp->dl_count, 1);
  294. nfsd4_init_callback(&dp->dl_recall);
  295. return dp;
  296. }
  297. void
  298. nfs4_put_delegation(struct nfs4_delegation *dp)
  299. {
  300. if (atomic_dec_and_test(&dp->dl_count)) {
  301. dprintk("NFSD: freeing dp %p\n",dp);
  302. put_nfs4_file(dp->dl_file);
  303. kmem_cache_free(deleg_slab, dp);
  304. num_delegations--;
  305. }
  306. }
  307. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  308. {
  309. if (atomic_dec_and_test(&fp->fi_delegees)) {
  310. vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
  311. fp->fi_lease = NULL;
  312. fput(fp->fi_deleg_file);
  313. fp->fi_deleg_file = NULL;
  314. }
  315. }
  316. static void unhash_stid(struct nfs4_stid *s)
  317. {
  318. struct idr *stateids = &s->sc_client->cl_stateids;
  319. idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
  320. }
  321. /* Called under the state lock. */
  322. static void
  323. unhash_delegation(struct nfs4_delegation *dp)
  324. {
  325. unhash_stid(&dp->dl_stid);
  326. list_del_init(&dp->dl_perclnt);
  327. spin_lock(&recall_lock);
  328. list_del_init(&dp->dl_perfile);
  329. list_del_init(&dp->dl_recall_lru);
  330. spin_unlock(&recall_lock);
  331. nfs4_put_deleg_lease(dp->dl_file);
  332. nfs4_put_delegation(dp);
  333. }
  334. /*
  335. * SETCLIENTID state
  336. */
  337. static unsigned int clientid_hashval(u32 id)
  338. {
  339. return id & CLIENT_HASH_MASK;
  340. }
  341. static unsigned int clientstr_hashval(const char *name)
  342. {
  343. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  344. }
  345. /*
  346. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  347. * st_{access,deny}_bmap field of the stateid, in order to track not
  348. * only what share bits are currently in force, but also what
  349. * combinations of share bits previous opens have used. This allows us
  350. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  351. * return an error if the client attempt to downgrade to a combination
  352. * of share bits not explicable by closing some of its previous opens.
  353. *
  354. * XXX: This enforcement is actually incomplete, since we don't keep
  355. * track of access/deny bit combinations; so, e.g., we allow:
  356. *
  357. * OPEN allow read, deny write
  358. * OPEN allow both, deny none
  359. * DOWNGRADE allow read, deny none
  360. *
  361. * which we should reject.
  362. */
  363. static unsigned int
  364. bmap_to_share_mode(unsigned long bmap) {
  365. int i;
  366. unsigned int access = 0;
  367. for (i = 1; i < 4; i++) {
  368. if (test_bit(i, &bmap))
  369. access |= i;
  370. }
  371. return access;
  372. }
  373. static bool
  374. test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
  375. unsigned int access, deny;
  376. access = bmap_to_share_mode(stp->st_access_bmap);
  377. deny = bmap_to_share_mode(stp->st_deny_bmap);
  378. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  379. return false;
  380. return true;
  381. }
  382. /* set share access for a given stateid */
  383. static inline void
  384. set_access(u32 access, struct nfs4_ol_stateid *stp)
  385. {
  386. __set_bit(access, &stp->st_access_bmap);
  387. }
  388. /* clear share access for a given stateid */
  389. static inline void
  390. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  391. {
  392. __clear_bit(access, &stp->st_access_bmap);
  393. }
  394. /* test whether a given stateid has access */
  395. static inline bool
  396. test_access(u32 access, struct nfs4_ol_stateid *stp)
  397. {
  398. return test_bit(access, &stp->st_access_bmap);
  399. }
  400. /* set share deny for a given stateid */
  401. static inline void
  402. set_deny(u32 access, struct nfs4_ol_stateid *stp)
  403. {
  404. __set_bit(access, &stp->st_deny_bmap);
  405. }
  406. /* clear share deny for a given stateid */
  407. static inline void
  408. clear_deny(u32 access, struct nfs4_ol_stateid *stp)
  409. {
  410. __clear_bit(access, &stp->st_deny_bmap);
  411. }
  412. /* test whether a given stateid is denying specific access */
  413. static inline bool
  414. test_deny(u32 access, struct nfs4_ol_stateid *stp)
  415. {
  416. return test_bit(access, &stp->st_deny_bmap);
  417. }
  418. static int nfs4_access_to_omode(u32 access)
  419. {
  420. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  421. case NFS4_SHARE_ACCESS_READ:
  422. return O_RDONLY;
  423. case NFS4_SHARE_ACCESS_WRITE:
  424. return O_WRONLY;
  425. case NFS4_SHARE_ACCESS_BOTH:
  426. return O_RDWR;
  427. }
  428. WARN_ON_ONCE(1);
  429. return O_RDONLY;
  430. }
  431. /* release all access and file references for a given stateid */
  432. static void
  433. release_all_access(struct nfs4_ol_stateid *stp)
  434. {
  435. int i;
  436. for (i = 1; i < 4; i++) {
  437. if (test_access(i, stp))
  438. nfs4_file_put_access(stp->st_file,
  439. nfs4_access_to_omode(i));
  440. clear_access(i, stp);
  441. }
  442. }
  443. static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
  444. {
  445. list_del(&stp->st_perfile);
  446. list_del(&stp->st_perstateowner);
  447. }
  448. static void close_generic_stateid(struct nfs4_ol_stateid *stp)
  449. {
  450. release_all_access(stp);
  451. put_nfs4_file(stp->st_file);
  452. stp->st_file = NULL;
  453. }
  454. static void free_generic_stateid(struct nfs4_ol_stateid *stp)
  455. {
  456. kmem_cache_free(stateid_slab, stp);
  457. }
  458. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  459. {
  460. struct file *file;
  461. unhash_generic_stateid(stp);
  462. unhash_stid(&stp->st_stid);
  463. file = find_any_file(stp->st_file);
  464. if (file)
  465. locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
  466. close_generic_stateid(stp);
  467. free_generic_stateid(stp);
  468. }
  469. static void unhash_lockowner(struct nfs4_lockowner *lo)
  470. {
  471. struct nfs4_ol_stateid *stp;
  472. list_del(&lo->lo_owner.so_strhash);
  473. list_del(&lo->lo_perstateid);
  474. list_del(&lo->lo_owner_ino_hash);
  475. while (!list_empty(&lo->lo_owner.so_stateids)) {
  476. stp = list_first_entry(&lo->lo_owner.so_stateids,
  477. struct nfs4_ol_stateid, st_perstateowner);
  478. release_lock_stateid(stp);
  479. }
  480. }
  481. static void release_lockowner(struct nfs4_lockowner *lo)
  482. {
  483. unhash_lockowner(lo);
  484. nfs4_free_lockowner(lo);
  485. }
  486. static void
  487. release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
  488. {
  489. struct nfs4_lockowner *lo;
  490. while (!list_empty(&open_stp->st_lockowners)) {
  491. lo = list_entry(open_stp->st_lockowners.next,
  492. struct nfs4_lockowner, lo_perstateid);
  493. release_lockowner(lo);
  494. }
  495. }
  496. static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
  497. {
  498. unhash_generic_stateid(stp);
  499. release_stateid_lockowners(stp);
  500. close_generic_stateid(stp);
  501. }
  502. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  503. {
  504. unhash_open_stateid(stp);
  505. unhash_stid(&stp->st_stid);
  506. free_generic_stateid(stp);
  507. }
  508. static void unhash_openowner(struct nfs4_openowner *oo)
  509. {
  510. struct nfs4_ol_stateid *stp;
  511. list_del(&oo->oo_owner.so_strhash);
  512. list_del(&oo->oo_perclient);
  513. while (!list_empty(&oo->oo_owner.so_stateids)) {
  514. stp = list_first_entry(&oo->oo_owner.so_stateids,
  515. struct nfs4_ol_stateid, st_perstateowner);
  516. release_open_stateid(stp);
  517. }
  518. }
  519. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  520. {
  521. struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
  522. if (s) {
  523. unhash_stid(&s->st_stid);
  524. free_generic_stateid(s);
  525. oo->oo_last_closed_stid = NULL;
  526. }
  527. }
  528. static void release_openowner(struct nfs4_openowner *oo)
  529. {
  530. unhash_openowner(oo);
  531. list_del(&oo->oo_close_lru);
  532. release_last_closed_stateid(oo);
  533. nfs4_free_openowner(oo);
  534. }
  535. static inline int
  536. hash_sessionid(struct nfs4_sessionid *sessionid)
  537. {
  538. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  539. return sid->sequence % SESSION_HASH_SIZE;
  540. }
  541. #ifdef NFSD_DEBUG
  542. static inline void
  543. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  544. {
  545. u32 *ptr = (u32 *)(&sessionid->data[0]);
  546. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  547. }
  548. #else
  549. static inline void
  550. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  551. {
  552. }
  553. #endif
  554. static void
  555. gen_sessionid(struct nfsd4_session *ses)
  556. {
  557. struct nfs4_client *clp = ses->se_client;
  558. struct nfsd4_sessionid *sid;
  559. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  560. sid->clientid = clp->cl_clientid;
  561. sid->sequence = current_sessionid++;
  562. sid->reserved = 0;
  563. }
  564. /*
  565. * The protocol defines ca_maxresponssize_cached to include the size of
  566. * the rpc header, but all we need to cache is the data starting after
  567. * the end of the initial SEQUENCE operation--the rest we regenerate
  568. * each time. Therefore we can advertise a ca_maxresponssize_cached
  569. * value that is the number of bytes in our cache plus a few additional
  570. * bytes. In order to stay on the safe side, and not promise more than
  571. * we can cache, those additional bytes must be the minimum possible: 24
  572. * bytes of rpc header (xid through accept state, with AUTH_NULL
  573. * verifier), 12 for the compound header (with zero-length tag), and 44
  574. * for the SEQUENCE op response:
  575. */
  576. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  577. static void
  578. free_session_slots(struct nfsd4_session *ses)
  579. {
  580. int i;
  581. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  582. kfree(ses->se_slots[i]);
  583. }
  584. /*
  585. * We don't actually need to cache the rpc and session headers, so we
  586. * can allocate a little less for each slot:
  587. */
  588. static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
  589. {
  590. return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  591. }
  592. static int nfsd4_sanitize_slot_size(u32 size)
  593. {
  594. size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
  595. size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
  596. return size;
  597. }
  598. /*
  599. * XXX: If we run out of reserved DRC memory we could (up to a point)
  600. * re-negotiate active sessions and reduce their slot usage to make
  601. * room for new connections. For now we just fail the create session.
  602. */
  603. static int nfsd4_get_drc_mem(int slotsize, u32 num)
  604. {
  605. int avail;
  606. num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
  607. spin_lock(&nfsd_drc_lock);
  608. avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
  609. nfsd_drc_max_mem - nfsd_drc_mem_used);
  610. num = min_t(int, num, avail / slotsize);
  611. nfsd_drc_mem_used += num * slotsize;
  612. spin_unlock(&nfsd_drc_lock);
  613. return num;
  614. }
  615. static void nfsd4_put_drc_mem(int slotsize, int num)
  616. {
  617. spin_lock(&nfsd_drc_lock);
  618. nfsd_drc_mem_used -= slotsize * num;
  619. spin_unlock(&nfsd_drc_lock);
  620. }
  621. static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
  622. {
  623. struct nfsd4_session *new;
  624. int mem, i;
  625. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  626. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  627. mem = numslots * sizeof(struct nfsd4_slot *);
  628. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  629. if (!new)
  630. return NULL;
  631. /* allocate each struct nfsd4_slot and data cache in one piece */
  632. for (i = 0; i < numslots; i++) {
  633. mem = sizeof(struct nfsd4_slot) + slotsize;
  634. new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
  635. if (!new->se_slots[i])
  636. goto out_free;
  637. }
  638. return new;
  639. out_free:
  640. while (i--)
  641. kfree(new->se_slots[i]);
  642. kfree(new);
  643. return NULL;
  644. }
  645. static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
  646. {
  647. u32 maxrpc = nfsd_serv->sv_max_mesg;
  648. new->maxreqs = numslots;
  649. new->maxresp_cached = min_t(u32, req->maxresp_cached,
  650. slotsize + NFSD_MIN_HDR_SEQ_SZ);
  651. new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
  652. new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
  653. new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  654. }
  655. static void free_conn(struct nfsd4_conn *c)
  656. {
  657. svc_xprt_put(c->cn_xprt);
  658. kfree(c);
  659. }
  660. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  661. {
  662. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  663. struct nfs4_client *clp = c->cn_session->se_client;
  664. spin_lock(&clp->cl_lock);
  665. if (!list_empty(&c->cn_persession)) {
  666. list_del(&c->cn_persession);
  667. free_conn(c);
  668. }
  669. spin_unlock(&clp->cl_lock);
  670. nfsd4_probe_callback(clp);
  671. }
  672. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  673. {
  674. struct nfsd4_conn *conn;
  675. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  676. if (!conn)
  677. return NULL;
  678. svc_xprt_get(rqstp->rq_xprt);
  679. conn->cn_xprt = rqstp->rq_xprt;
  680. conn->cn_flags = flags;
  681. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  682. return conn;
  683. }
  684. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  685. {
  686. conn->cn_session = ses;
  687. list_add(&conn->cn_persession, &ses->se_conns);
  688. }
  689. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  690. {
  691. struct nfs4_client *clp = ses->se_client;
  692. spin_lock(&clp->cl_lock);
  693. __nfsd4_hash_conn(conn, ses);
  694. spin_unlock(&clp->cl_lock);
  695. }
  696. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  697. {
  698. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  699. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  700. }
  701. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  702. {
  703. int ret;
  704. nfsd4_hash_conn(conn, ses);
  705. ret = nfsd4_register_conn(conn);
  706. if (ret)
  707. /* oops; xprt is already down: */
  708. nfsd4_conn_lost(&conn->cn_xpt_user);
  709. if (conn->cn_flags & NFS4_CDFC4_BACK) {
  710. /* callback channel may be back up */
  711. nfsd4_probe_callback(ses->se_client);
  712. }
  713. }
  714. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  715. {
  716. u32 dir = NFS4_CDFC4_FORE;
  717. if (cses->flags & SESSION4_BACK_CHAN)
  718. dir |= NFS4_CDFC4_BACK;
  719. return alloc_conn(rqstp, dir);
  720. }
  721. /* must be called under client_lock */
  722. static void nfsd4_del_conns(struct nfsd4_session *s)
  723. {
  724. struct nfs4_client *clp = s->se_client;
  725. struct nfsd4_conn *c;
  726. spin_lock(&clp->cl_lock);
  727. while (!list_empty(&s->se_conns)) {
  728. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  729. list_del_init(&c->cn_persession);
  730. spin_unlock(&clp->cl_lock);
  731. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  732. free_conn(c);
  733. spin_lock(&clp->cl_lock);
  734. }
  735. spin_unlock(&clp->cl_lock);
  736. }
  737. static void __free_session(struct nfsd4_session *ses)
  738. {
  739. nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
  740. free_session_slots(ses);
  741. kfree(ses);
  742. }
  743. static void free_session(struct kref *kref)
  744. {
  745. struct nfsd4_session *ses;
  746. struct nfsd_net *nn;
  747. ses = container_of(kref, struct nfsd4_session, se_ref);
  748. nn = net_generic(ses->se_client->net, nfsd_net_id);
  749. lockdep_assert_held(&nn->client_lock);
  750. nfsd4_del_conns(ses);
  751. __free_session(ses);
  752. }
  753. void nfsd4_put_session(struct nfsd4_session *ses)
  754. {
  755. struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
  756. spin_lock(&nn->client_lock);
  757. nfsd4_put_session_locked(ses);
  758. spin_unlock(&nn->client_lock);
  759. }
  760. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan)
  761. {
  762. struct nfsd4_session *new;
  763. int numslots, slotsize;
  764. /*
  765. * Note decreasing slot size below client's request may
  766. * make it difficult for client to function correctly, whereas
  767. * decreasing the number of slots will (just?) affect
  768. * performance. When short on memory we therefore prefer to
  769. * decrease number of slots instead of their size.
  770. */
  771. slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
  772. numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
  773. if (numslots < 1)
  774. return NULL;
  775. new = __alloc_session(slotsize, numslots);
  776. if (!new) {
  777. nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
  778. return NULL;
  779. }
  780. init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
  781. return new;
  782. }
  783. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  784. {
  785. int idx;
  786. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  787. new->se_client = clp;
  788. gen_sessionid(new);
  789. INIT_LIST_HEAD(&new->se_conns);
  790. new->se_cb_seq_nr = 1;
  791. new->se_flags = cses->flags;
  792. new->se_cb_prog = cses->callback_prog;
  793. new->se_cb_sec = cses->cb_sec;
  794. kref_init(&new->se_ref);
  795. idx = hash_sessionid(&new->se_sessionid);
  796. spin_lock(&nn->client_lock);
  797. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  798. spin_lock(&clp->cl_lock);
  799. list_add(&new->se_perclnt, &clp->cl_sessions);
  800. spin_unlock(&clp->cl_lock);
  801. spin_unlock(&nn->client_lock);
  802. if (cses->flags & SESSION4_BACK_CHAN) {
  803. struct sockaddr *sa = svc_addr(rqstp);
  804. /*
  805. * This is a little silly; with sessions there's no real
  806. * use for the callback address. Use the peer address
  807. * as a reasonable default for now, but consider fixing
  808. * the rpc client not to require an address in the
  809. * future:
  810. */
  811. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  812. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  813. }
  814. }
  815. /* caller must hold client_lock */
  816. static struct nfsd4_session *
  817. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  818. {
  819. struct nfsd4_session *elem;
  820. int idx;
  821. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  822. dump_sessionid(__func__, sessionid);
  823. idx = hash_sessionid(sessionid);
  824. /* Search in the appropriate list */
  825. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  826. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  827. NFS4_MAX_SESSIONID_LEN)) {
  828. return elem;
  829. }
  830. }
  831. dprintk("%s: session not found\n", __func__);
  832. return NULL;
  833. }
  834. /* caller must hold client_lock */
  835. static void
  836. unhash_session(struct nfsd4_session *ses)
  837. {
  838. list_del(&ses->se_hash);
  839. spin_lock(&ses->se_client->cl_lock);
  840. list_del(&ses->se_perclnt);
  841. spin_unlock(&ses->se_client->cl_lock);
  842. }
  843. /* must be called under the client_lock */
  844. static inline void
  845. renew_client_locked(struct nfs4_client *clp)
  846. {
  847. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  848. if (is_client_expired(clp)) {
  849. WARN_ON(1);
  850. printk("%s: client (clientid %08x/%08x) already expired\n",
  851. __func__,
  852. clp->cl_clientid.cl_boot,
  853. clp->cl_clientid.cl_id);
  854. return;
  855. }
  856. dprintk("renewing client (clientid %08x/%08x)\n",
  857. clp->cl_clientid.cl_boot,
  858. clp->cl_clientid.cl_id);
  859. list_move_tail(&clp->cl_lru, &nn->client_lru);
  860. clp->cl_time = get_seconds();
  861. }
  862. static inline void
  863. renew_client(struct nfs4_client *clp)
  864. {
  865. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  866. spin_lock(&nn->client_lock);
  867. renew_client_locked(clp);
  868. spin_unlock(&nn->client_lock);
  869. }
  870. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  871. static int
  872. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  873. {
  874. if (clid->cl_boot == nn->boot_time)
  875. return 0;
  876. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  877. clid->cl_boot, clid->cl_id, nn->boot_time);
  878. return 1;
  879. }
  880. /*
  881. * XXX Should we use a slab cache ?
  882. * This type of memory management is somewhat inefficient, but we use it
  883. * anyway since SETCLIENTID is not a common operation.
  884. */
  885. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  886. {
  887. struct nfs4_client *clp;
  888. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  889. if (clp == NULL)
  890. return NULL;
  891. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  892. if (clp->cl_name.data == NULL) {
  893. kfree(clp);
  894. return NULL;
  895. }
  896. clp->cl_name.len = name.len;
  897. return clp;
  898. }
  899. static inline void
  900. free_client(struct nfs4_client *clp)
  901. {
  902. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  903. lockdep_assert_held(&nn->client_lock);
  904. while (!list_empty(&clp->cl_sessions)) {
  905. struct nfsd4_session *ses;
  906. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  907. se_perclnt);
  908. list_del(&ses->se_perclnt);
  909. nfsd4_put_session_locked(ses);
  910. }
  911. free_svc_cred(&clp->cl_cred);
  912. kfree(clp->cl_name.data);
  913. kfree(clp);
  914. }
  915. void
  916. release_session_client(struct nfsd4_session *session)
  917. {
  918. struct nfs4_client *clp = session->se_client;
  919. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  920. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  921. return;
  922. if (is_client_expired(clp)) {
  923. free_client(clp);
  924. session->se_client = NULL;
  925. } else
  926. renew_client_locked(clp);
  927. spin_unlock(&nn->client_lock);
  928. }
  929. /* must be called under the client_lock */
  930. static inline void
  931. unhash_client_locked(struct nfs4_client *clp)
  932. {
  933. struct nfsd4_session *ses;
  934. mark_client_expired(clp);
  935. list_del(&clp->cl_lru);
  936. spin_lock(&clp->cl_lock);
  937. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  938. list_del_init(&ses->se_hash);
  939. spin_unlock(&clp->cl_lock);
  940. }
  941. static void
  942. destroy_client(struct nfs4_client *clp)
  943. {
  944. struct nfs4_openowner *oo;
  945. struct nfs4_delegation *dp;
  946. struct list_head reaplist;
  947. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  948. INIT_LIST_HEAD(&reaplist);
  949. spin_lock(&recall_lock);
  950. while (!list_empty(&clp->cl_delegations)) {
  951. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  952. list_del_init(&dp->dl_perclnt);
  953. list_move(&dp->dl_recall_lru, &reaplist);
  954. }
  955. spin_unlock(&recall_lock);
  956. while (!list_empty(&reaplist)) {
  957. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  958. unhash_delegation(dp);
  959. }
  960. while (!list_empty(&clp->cl_openowners)) {
  961. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  962. release_openowner(oo);
  963. }
  964. nfsd4_shutdown_callback(clp);
  965. if (clp->cl_cb_conn.cb_xprt)
  966. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  967. list_del(&clp->cl_idhash);
  968. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  969. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  970. else
  971. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  972. spin_lock(&nn->client_lock);
  973. unhash_client_locked(clp);
  974. if (atomic_read(&clp->cl_refcount) == 0)
  975. free_client(clp);
  976. spin_unlock(&nn->client_lock);
  977. }
  978. static void expire_client(struct nfs4_client *clp)
  979. {
  980. nfsd4_client_record_remove(clp);
  981. destroy_client(clp);
  982. }
  983. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  984. {
  985. memcpy(target->cl_verifier.data, source->data,
  986. sizeof(target->cl_verifier.data));
  987. }
  988. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  989. {
  990. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  991. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  992. }
  993. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  994. {
  995. if (source->cr_principal) {
  996. target->cr_principal =
  997. kstrdup(source->cr_principal, GFP_KERNEL);
  998. if (target->cr_principal == NULL)
  999. return -ENOMEM;
  1000. } else
  1001. target->cr_principal = NULL;
  1002. target->cr_flavor = source->cr_flavor;
  1003. target->cr_uid = source->cr_uid;
  1004. target->cr_gid = source->cr_gid;
  1005. target->cr_group_info = source->cr_group_info;
  1006. get_group_info(target->cr_group_info);
  1007. return 0;
  1008. }
  1009. static long long
  1010. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1011. {
  1012. long long res;
  1013. res = o1->len - o2->len;
  1014. if (res)
  1015. return res;
  1016. return (long long)memcmp(o1->data, o2->data, o1->len);
  1017. }
  1018. static int same_name(const char *n1, const char *n2)
  1019. {
  1020. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1021. }
  1022. static int
  1023. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1024. {
  1025. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1026. }
  1027. static int
  1028. same_clid(clientid_t *cl1, clientid_t *cl2)
  1029. {
  1030. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1031. }
  1032. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1033. {
  1034. int i;
  1035. if (g1->ngroups != g2->ngroups)
  1036. return false;
  1037. for (i=0; i<g1->ngroups; i++)
  1038. if (GROUP_AT(g1, i) != GROUP_AT(g2, i))
  1039. return false;
  1040. return true;
  1041. }
  1042. /*
  1043. * RFC 3530 language requires clid_inuse be returned when the
  1044. * "principal" associated with a requests differs from that previously
  1045. * used. We use uid, gid's, and gss principal string as our best
  1046. * approximation. We also don't want to allow non-gss use of a client
  1047. * established using gss: in theory cr_principal should catch that
  1048. * change, but in practice cr_principal can be null even in the gss case
  1049. * since gssd doesn't always pass down a principal string.
  1050. */
  1051. static bool is_gss_cred(struct svc_cred *cr)
  1052. {
  1053. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1054. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1055. }
  1056. static bool
  1057. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1058. {
  1059. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1060. || (cr1->cr_uid != cr2->cr_uid)
  1061. || (cr1->cr_gid != cr2->cr_gid)
  1062. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1063. return false;
  1064. if (cr1->cr_principal == cr2->cr_principal)
  1065. return true;
  1066. if (!cr1->cr_principal || !cr2->cr_principal)
  1067. return false;
  1068. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1069. }
  1070. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1071. {
  1072. static u32 current_clientid = 1;
  1073. clp->cl_clientid.cl_boot = nn->boot_time;
  1074. clp->cl_clientid.cl_id = current_clientid++;
  1075. }
  1076. static void gen_confirm(struct nfs4_client *clp)
  1077. {
  1078. __be32 verf[2];
  1079. static u32 i;
  1080. verf[0] = (__be32)get_seconds();
  1081. verf[1] = (__be32)i++;
  1082. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1083. }
  1084. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1085. {
  1086. return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1087. }
  1088. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1089. {
  1090. struct nfs4_stid *s;
  1091. s = find_stateid(cl, t);
  1092. if (!s)
  1093. return NULL;
  1094. if (typemask & s->sc_type)
  1095. return s;
  1096. return NULL;
  1097. }
  1098. static struct nfs4_client *create_client(struct xdr_netobj name,
  1099. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1100. {
  1101. struct nfs4_client *clp;
  1102. struct sockaddr *sa = svc_addr(rqstp);
  1103. int ret;
  1104. struct net *net = SVC_NET(rqstp);
  1105. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1106. clp = alloc_client(name);
  1107. if (clp == NULL)
  1108. return NULL;
  1109. INIT_LIST_HEAD(&clp->cl_sessions);
  1110. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1111. if (ret) {
  1112. spin_lock(&nn->client_lock);
  1113. free_client(clp);
  1114. spin_unlock(&nn->client_lock);
  1115. return NULL;
  1116. }
  1117. idr_init(&clp->cl_stateids);
  1118. atomic_set(&clp->cl_refcount, 0);
  1119. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1120. INIT_LIST_HEAD(&clp->cl_idhash);
  1121. INIT_LIST_HEAD(&clp->cl_openowners);
  1122. INIT_LIST_HEAD(&clp->cl_delegations);
  1123. INIT_LIST_HEAD(&clp->cl_lru);
  1124. INIT_LIST_HEAD(&clp->cl_callbacks);
  1125. spin_lock_init(&clp->cl_lock);
  1126. nfsd4_init_callback(&clp->cl_cb_null);
  1127. clp->cl_time = get_seconds();
  1128. clear_bit(0, &clp->cl_cb_slot_busy);
  1129. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1130. copy_verf(clp, verf);
  1131. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1132. gen_confirm(clp);
  1133. clp->cl_cb_session = NULL;
  1134. clp->net = net;
  1135. return clp;
  1136. }
  1137. static void
  1138. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1139. {
  1140. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1141. struct nfs4_client *clp;
  1142. while (*new) {
  1143. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1144. parent = *new;
  1145. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1146. new = &((*new)->rb_left);
  1147. else
  1148. new = &((*new)->rb_right);
  1149. }
  1150. rb_link_node(&new_clp->cl_namenode, parent, new);
  1151. rb_insert_color(&new_clp->cl_namenode, root);
  1152. }
  1153. static struct nfs4_client *
  1154. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1155. {
  1156. long long cmp;
  1157. struct rb_node *node = root->rb_node;
  1158. struct nfs4_client *clp;
  1159. while (node) {
  1160. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1161. cmp = compare_blob(&clp->cl_name, name);
  1162. if (cmp > 0)
  1163. node = node->rb_left;
  1164. else if (cmp < 0)
  1165. node = node->rb_right;
  1166. else
  1167. return clp;
  1168. }
  1169. return NULL;
  1170. }
  1171. static void
  1172. add_to_unconfirmed(struct nfs4_client *clp)
  1173. {
  1174. unsigned int idhashval;
  1175. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1176. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1177. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1178. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1179. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1180. renew_client(clp);
  1181. }
  1182. static void
  1183. move_to_confirmed(struct nfs4_client *clp)
  1184. {
  1185. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1186. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1187. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1188. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1189. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1190. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1191. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1192. renew_client(clp);
  1193. }
  1194. static struct nfs4_client *
  1195. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1196. {
  1197. struct nfs4_client *clp;
  1198. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1199. list_for_each_entry(clp, &nn->conf_id_hashtbl[idhashval], cl_idhash) {
  1200. if (same_clid(&clp->cl_clientid, clid)) {
  1201. if ((bool)clp->cl_minorversion != sessions)
  1202. return NULL;
  1203. renew_client(clp);
  1204. return clp;
  1205. }
  1206. }
  1207. return NULL;
  1208. }
  1209. static struct nfs4_client *
  1210. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1211. {
  1212. struct nfs4_client *clp;
  1213. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1214. list_for_each_entry(clp, &nn->unconf_id_hashtbl[idhashval], cl_idhash) {
  1215. if (same_clid(&clp->cl_clientid, clid)) {
  1216. if ((bool)clp->cl_minorversion != sessions)
  1217. return NULL;
  1218. return clp;
  1219. }
  1220. }
  1221. return NULL;
  1222. }
  1223. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1224. {
  1225. return clp->cl_exchange_flags != 0;
  1226. }
  1227. static struct nfs4_client *
  1228. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1229. {
  1230. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1231. }
  1232. static struct nfs4_client *
  1233. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1234. {
  1235. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1236. }
  1237. static void
  1238. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1239. {
  1240. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1241. struct sockaddr *sa = svc_addr(rqstp);
  1242. u32 scopeid = rpc_get_scope_id(sa);
  1243. unsigned short expected_family;
  1244. /* Currently, we only support tcp and tcp6 for the callback channel */
  1245. if (se->se_callback_netid_len == 3 &&
  1246. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1247. expected_family = AF_INET;
  1248. else if (se->se_callback_netid_len == 4 &&
  1249. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1250. expected_family = AF_INET6;
  1251. else
  1252. goto out_err;
  1253. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1254. se->se_callback_addr_len,
  1255. (struct sockaddr *)&conn->cb_addr,
  1256. sizeof(conn->cb_addr));
  1257. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1258. goto out_err;
  1259. if (conn->cb_addr.ss_family == AF_INET6)
  1260. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1261. conn->cb_prog = se->se_callback_prog;
  1262. conn->cb_ident = se->se_callback_ident;
  1263. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1264. return;
  1265. out_err:
  1266. conn->cb_addr.ss_family = AF_UNSPEC;
  1267. conn->cb_addrlen = 0;
  1268. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1269. "will not receive delegations\n",
  1270. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1271. return;
  1272. }
  1273. /*
  1274. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1275. */
  1276. void
  1277. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1278. {
  1279. struct nfsd4_slot *slot = resp->cstate.slot;
  1280. unsigned int base;
  1281. dprintk("--> %s slot %p\n", __func__, slot);
  1282. slot->sl_opcnt = resp->opcnt;
  1283. slot->sl_status = resp->cstate.status;
  1284. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1285. if (nfsd4_not_cached(resp)) {
  1286. slot->sl_datalen = 0;
  1287. return;
  1288. }
  1289. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1290. base = (char *)resp->cstate.datap -
  1291. (char *)resp->xbuf->head[0].iov_base;
  1292. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1293. slot->sl_datalen))
  1294. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1295. return;
  1296. }
  1297. /*
  1298. * Encode the replay sequence operation from the slot values.
  1299. * If cachethis is FALSE encode the uncached rep error on the next
  1300. * operation which sets resp->p and increments resp->opcnt for
  1301. * nfs4svc_encode_compoundres.
  1302. *
  1303. */
  1304. static __be32
  1305. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1306. struct nfsd4_compoundres *resp)
  1307. {
  1308. struct nfsd4_op *op;
  1309. struct nfsd4_slot *slot = resp->cstate.slot;
  1310. /* Encode the replayed sequence operation */
  1311. op = &args->ops[resp->opcnt - 1];
  1312. nfsd4_encode_operation(resp, op);
  1313. /* Return nfserr_retry_uncached_rep in next operation. */
  1314. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1315. op = &args->ops[resp->opcnt++];
  1316. op->status = nfserr_retry_uncached_rep;
  1317. nfsd4_encode_operation(resp, op);
  1318. }
  1319. return op->status;
  1320. }
  1321. /*
  1322. * The sequence operation is not cached because we can use the slot and
  1323. * session values.
  1324. */
  1325. __be32
  1326. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1327. struct nfsd4_sequence *seq)
  1328. {
  1329. struct nfsd4_slot *slot = resp->cstate.slot;
  1330. __be32 status;
  1331. dprintk("--> %s slot %p\n", __func__, slot);
  1332. /* Either returns 0 or nfserr_retry_uncached */
  1333. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1334. if (status == nfserr_retry_uncached_rep)
  1335. return status;
  1336. /* The sequence operation has been encoded, cstate->datap set. */
  1337. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1338. resp->opcnt = slot->sl_opcnt;
  1339. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1340. status = slot->sl_status;
  1341. return status;
  1342. }
  1343. /*
  1344. * Set the exchange_id flags returned by the server.
  1345. */
  1346. static void
  1347. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1348. {
  1349. /* pNFS is not supported */
  1350. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1351. /* Referrals are supported, Migration is not. */
  1352. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1353. /* set the wire flags to return to client. */
  1354. clid->flags = new->cl_exchange_flags;
  1355. }
  1356. static bool client_has_state(struct nfs4_client *clp)
  1357. {
  1358. /*
  1359. * Note clp->cl_openowners check isn't quite right: there's no
  1360. * need to count owners without stateid's.
  1361. *
  1362. * Also note we should probably be using this in 4.0 case too.
  1363. */
  1364. return !list_empty(&clp->cl_openowners)
  1365. || !list_empty(&clp->cl_delegations)
  1366. || !list_empty(&clp->cl_sessions);
  1367. }
  1368. __be32
  1369. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1370. struct nfsd4_compound_state *cstate,
  1371. struct nfsd4_exchange_id *exid)
  1372. {
  1373. struct nfs4_client *unconf, *conf, *new;
  1374. __be32 status;
  1375. char addr_str[INET6_ADDRSTRLEN];
  1376. nfs4_verifier verf = exid->verifier;
  1377. struct sockaddr *sa = svc_addr(rqstp);
  1378. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1379. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1380. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1381. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1382. "ip_addr=%s flags %x, spa_how %d\n",
  1383. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1384. addr_str, exid->flags, exid->spa_how);
  1385. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1386. return nfserr_inval;
  1387. /* Currently only support SP4_NONE */
  1388. switch (exid->spa_how) {
  1389. case SP4_NONE:
  1390. break;
  1391. default: /* checked by xdr code */
  1392. WARN_ON_ONCE(1);
  1393. case SP4_SSV:
  1394. case SP4_MACH_CRED:
  1395. return nfserr_serverfault; /* no excuse :-/ */
  1396. }
  1397. /* Cases below refer to rfc 5661 section 18.35.4: */
  1398. nfs4_lock_state();
  1399. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1400. if (conf) {
  1401. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1402. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1403. if (update) {
  1404. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1405. status = nfserr_inval;
  1406. goto out;
  1407. }
  1408. if (!creds_match) { /* case 9 */
  1409. status = nfserr_perm;
  1410. goto out;
  1411. }
  1412. if (!verfs_match) { /* case 8 */
  1413. status = nfserr_not_same;
  1414. goto out;
  1415. }
  1416. /* case 6 */
  1417. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1418. new = conf;
  1419. goto out_copy;
  1420. }
  1421. if (!creds_match) { /* case 3 */
  1422. if (client_has_state(conf)) {
  1423. status = nfserr_clid_inuse;
  1424. goto out;
  1425. }
  1426. expire_client(conf);
  1427. goto out_new;
  1428. }
  1429. if (verfs_match) { /* case 2 */
  1430. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1431. new = conf;
  1432. goto out_copy;
  1433. }
  1434. /* case 5, client reboot */
  1435. goto out_new;
  1436. }
  1437. if (update) { /* case 7 */
  1438. status = nfserr_noent;
  1439. goto out;
  1440. }
  1441. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1442. if (unconf) /* case 4, possible retry or client restart */
  1443. expire_client(unconf);
  1444. /* case 1 (normal case) */
  1445. out_new:
  1446. new = create_client(exid->clname, rqstp, &verf);
  1447. if (new == NULL) {
  1448. status = nfserr_jukebox;
  1449. goto out;
  1450. }
  1451. new->cl_minorversion = 1;
  1452. gen_clid(new, nn);
  1453. add_to_unconfirmed(new);
  1454. out_copy:
  1455. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1456. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1457. exid->seqid = new->cl_cs_slot.sl_seqid + 1;
  1458. nfsd4_set_ex_flags(new, exid);
  1459. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1460. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1461. status = nfs_ok;
  1462. out:
  1463. nfs4_unlock_state();
  1464. return status;
  1465. }
  1466. static __be32
  1467. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1468. {
  1469. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1470. slot_seqid);
  1471. /* The slot is in use, and no response has been sent. */
  1472. if (slot_inuse) {
  1473. if (seqid == slot_seqid)
  1474. return nfserr_jukebox;
  1475. else
  1476. return nfserr_seq_misordered;
  1477. }
  1478. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1479. if (likely(seqid == slot_seqid + 1))
  1480. return nfs_ok;
  1481. if (seqid == slot_seqid)
  1482. return nfserr_replay_cache;
  1483. return nfserr_seq_misordered;
  1484. }
  1485. /*
  1486. * Cache the create session result into the create session single DRC
  1487. * slot cache by saving the xdr structure. sl_seqid has been set.
  1488. * Do this for solo or embedded create session operations.
  1489. */
  1490. static void
  1491. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1492. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1493. {
  1494. slot->sl_status = nfserr;
  1495. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1496. }
  1497. static __be32
  1498. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1499. struct nfsd4_clid_slot *slot)
  1500. {
  1501. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1502. return slot->sl_status;
  1503. }
  1504. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1505. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1506. 1 + /* MIN tag is length with zero, only length */ \
  1507. 3 + /* version, opcount, opcode */ \
  1508. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1509. /* seqid, slotID, slotID, cache */ \
  1510. 4 ) * sizeof(__be32))
  1511. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1512. 2 + /* verifier: AUTH_NULL, length 0 */\
  1513. 1 + /* status */ \
  1514. 1 + /* MIN tag is length with zero, only length */ \
  1515. 3 + /* opcount, opcode, opstatus*/ \
  1516. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1517. /* seqid, slotID, slotID, slotID, status */ \
  1518. 5 ) * sizeof(__be32))
  1519. static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
  1520. {
  1521. return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
  1522. || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
  1523. }
  1524. __be32
  1525. nfsd4_create_session(struct svc_rqst *rqstp,
  1526. struct nfsd4_compound_state *cstate,
  1527. struct nfsd4_create_session *cr_ses)
  1528. {
  1529. struct sockaddr *sa = svc_addr(rqstp);
  1530. struct nfs4_client *conf, *unconf;
  1531. struct nfsd4_session *new;
  1532. struct nfsd4_conn *conn;
  1533. struct nfsd4_clid_slot *cs_slot = NULL;
  1534. __be32 status = 0;
  1535. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1536. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1537. return nfserr_inval;
  1538. if (check_forechannel_attrs(cr_ses->fore_channel))
  1539. return nfserr_toosmall;
  1540. new = alloc_session(&cr_ses->fore_channel);
  1541. if (!new)
  1542. return nfserr_jukebox;
  1543. status = nfserr_jukebox;
  1544. conn = alloc_conn_from_crses(rqstp, cr_ses);
  1545. if (!conn)
  1546. goto out_free_session;
  1547. nfs4_lock_state();
  1548. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  1549. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  1550. if (conf) {
  1551. cs_slot = &conf->cl_cs_slot;
  1552. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1553. if (status == nfserr_replay_cache) {
  1554. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1555. goto out_free_conn;
  1556. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1557. status = nfserr_seq_misordered;
  1558. goto out_free_conn;
  1559. }
  1560. } else if (unconf) {
  1561. struct nfs4_client *old;
  1562. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1563. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1564. status = nfserr_clid_inuse;
  1565. goto out_free_conn;
  1566. }
  1567. cs_slot = &unconf->cl_cs_slot;
  1568. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1569. if (status) {
  1570. /* an unconfirmed replay returns misordered */
  1571. status = nfserr_seq_misordered;
  1572. goto out_free_conn;
  1573. }
  1574. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1575. if (old)
  1576. expire_client(old);
  1577. move_to_confirmed(unconf);
  1578. conf = unconf;
  1579. } else {
  1580. status = nfserr_stale_clientid;
  1581. goto out_free_conn;
  1582. }
  1583. status = nfs_ok;
  1584. /*
  1585. * We do not support RDMA or persistent sessions
  1586. */
  1587. cr_ses->flags &= ~SESSION4_PERSIST;
  1588. cr_ses->flags &= ~SESSION4_RDMA;
  1589. init_session(rqstp, new, conf, cr_ses);
  1590. nfsd4_init_conn(rqstp, conn, new);
  1591. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1592. NFS4_MAX_SESSIONID_LEN);
  1593. memcpy(&cr_ses->fore_channel, &new->se_fchannel,
  1594. sizeof(struct nfsd4_channel_attrs));
  1595. cs_slot->sl_seqid++;
  1596. cr_ses->seqid = cs_slot->sl_seqid;
  1597. /* cache solo and embedded create sessions under the state lock */
  1598. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1599. out:
  1600. nfs4_unlock_state();
  1601. dprintk("%s returns %d\n", __func__, ntohl(status));
  1602. return status;
  1603. out_free_conn:
  1604. free_conn(conn);
  1605. out_free_session:
  1606. __free_session(new);
  1607. goto out;
  1608. }
  1609. static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
  1610. {
  1611. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1612. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1613. return argp->opcnt == resp->opcnt;
  1614. }
  1615. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1616. {
  1617. switch (*dir) {
  1618. case NFS4_CDFC4_FORE:
  1619. case NFS4_CDFC4_BACK:
  1620. return nfs_ok;
  1621. case NFS4_CDFC4_FORE_OR_BOTH:
  1622. case NFS4_CDFC4_BACK_OR_BOTH:
  1623. *dir = NFS4_CDFC4_BOTH;
  1624. return nfs_ok;
  1625. };
  1626. return nfserr_inval;
  1627. }
  1628. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  1629. {
  1630. struct nfsd4_session *session = cstate->session;
  1631. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1632. spin_lock(&nn->client_lock);
  1633. session->se_cb_prog = bc->bc_cb_program;
  1634. session->se_cb_sec = bc->bc_cb_sec;
  1635. spin_unlock(&nn->client_lock);
  1636. nfsd4_probe_callback(session->se_client);
  1637. return nfs_ok;
  1638. }
  1639. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1640. struct nfsd4_compound_state *cstate,
  1641. struct nfsd4_bind_conn_to_session *bcts)
  1642. {
  1643. __be32 status;
  1644. struct nfsd4_conn *conn;
  1645. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1646. if (!nfsd4_last_compound_op(rqstp))
  1647. return nfserr_not_only_op;
  1648. spin_lock(&nn->client_lock);
  1649. cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
  1650. /* Sorta weird: we only need the refcnt'ing because new_conn acquires
  1651. * client_lock iself: */
  1652. if (cstate->session) {
  1653. nfsd4_get_session(cstate->session);
  1654. atomic_inc(&cstate->session->se_client->cl_refcount);
  1655. }
  1656. spin_unlock(&nn->client_lock);
  1657. if (!cstate->session)
  1658. return nfserr_badsession;
  1659. status = nfsd4_map_bcts_dir(&bcts->dir);
  1660. if (status)
  1661. return status;
  1662. conn = alloc_conn(rqstp, bcts->dir);
  1663. if (!conn)
  1664. return nfserr_jukebox;
  1665. nfsd4_init_conn(rqstp, conn, cstate->session);
  1666. return nfs_ok;
  1667. }
  1668. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1669. {
  1670. if (!session)
  1671. return 0;
  1672. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1673. }
  1674. __be32
  1675. nfsd4_destroy_session(struct svc_rqst *r,
  1676. struct nfsd4_compound_state *cstate,
  1677. struct nfsd4_destroy_session *sessionid)
  1678. {
  1679. struct nfsd4_session *ses;
  1680. __be32 status = nfserr_badsession;
  1681. struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
  1682. /* Notes:
  1683. * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
  1684. * - Should we return nfserr_back_chan_busy if waiting for
  1685. * callbacks on to-be-destroyed session?
  1686. * - Do we need to clear any callback info from previous session?
  1687. */
  1688. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1689. if (!nfsd4_last_compound_op(r))
  1690. return nfserr_not_only_op;
  1691. }
  1692. dump_sessionid(__func__, &sessionid->sessionid);
  1693. spin_lock(&nn->client_lock);
  1694. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
  1695. if (!ses) {
  1696. spin_unlock(&nn->client_lock);
  1697. goto out;
  1698. }
  1699. unhash_session(ses);
  1700. spin_unlock(&nn->client_lock);
  1701. nfs4_lock_state();
  1702. nfsd4_probe_callback_sync(ses->se_client);
  1703. nfs4_unlock_state();
  1704. spin_lock(&nn->client_lock);
  1705. nfsd4_del_conns(ses);
  1706. nfsd4_put_session_locked(ses);
  1707. spin_unlock(&nn->client_lock);
  1708. status = nfs_ok;
  1709. out:
  1710. dprintk("%s returns %d\n", __func__, ntohl(status));
  1711. return status;
  1712. }
  1713. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1714. {
  1715. struct nfsd4_conn *c;
  1716. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1717. if (c->cn_xprt == xpt) {
  1718. return c;
  1719. }
  1720. }
  1721. return NULL;
  1722. }
  1723. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1724. {
  1725. struct nfs4_client *clp = ses->se_client;
  1726. struct nfsd4_conn *c;
  1727. int ret;
  1728. spin_lock(&clp->cl_lock);
  1729. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1730. if (c) {
  1731. spin_unlock(&clp->cl_lock);
  1732. free_conn(new);
  1733. return;
  1734. }
  1735. __nfsd4_hash_conn(new, ses);
  1736. spin_unlock(&clp->cl_lock);
  1737. ret = nfsd4_register_conn(new);
  1738. if (ret)
  1739. /* oops; xprt is already down: */
  1740. nfsd4_conn_lost(&new->cn_xpt_user);
  1741. return;
  1742. }
  1743. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1744. {
  1745. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1746. return args->opcnt > session->se_fchannel.maxops;
  1747. }
  1748. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1749. struct nfsd4_session *session)
  1750. {
  1751. struct xdr_buf *xb = &rqstp->rq_arg;
  1752. return xb->len > session->se_fchannel.maxreq_sz;
  1753. }
  1754. __be32
  1755. nfsd4_sequence(struct svc_rqst *rqstp,
  1756. struct nfsd4_compound_state *cstate,
  1757. struct nfsd4_sequence *seq)
  1758. {
  1759. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1760. struct nfsd4_session *session;
  1761. struct nfsd4_slot *slot;
  1762. struct nfsd4_conn *conn;
  1763. __be32 status;
  1764. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1765. if (resp->opcnt != 1)
  1766. return nfserr_sequence_pos;
  1767. /*
  1768. * Will be either used or freed by nfsd4_sequence_check_conn
  1769. * below.
  1770. */
  1771. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1772. if (!conn)
  1773. return nfserr_jukebox;
  1774. spin_lock(&nn->client_lock);
  1775. status = nfserr_badsession;
  1776. session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
  1777. if (!session)
  1778. goto out;
  1779. status = nfserr_too_many_ops;
  1780. if (nfsd4_session_too_many_ops(rqstp, session))
  1781. goto out;
  1782. status = nfserr_req_too_big;
  1783. if (nfsd4_request_too_big(rqstp, session))
  1784. goto out;
  1785. status = nfserr_badslot;
  1786. if (seq->slotid >= session->se_fchannel.maxreqs)
  1787. goto out;
  1788. slot = session->se_slots[seq->slotid];
  1789. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1790. /* We do not negotiate the number of slots yet, so set the
  1791. * maxslots to the session maxreqs which is used to encode
  1792. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1793. seq->maxslots = session->se_fchannel.maxreqs;
  1794. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1795. slot->sl_flags & NFSD4_SLOT_INUSE);
  1796. if (status == nfserr_replay_cache) {
  1797. status = nfserr_seq_misordered;
  1798. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1799. goto out;
  1800. cstate->slot = slot;
  1801. cstate->session = session;
  1802. /* Return the cached reply status and set cstate->status
  1803. * for nfsd4_proc_compound processing */
  1804. status = nfsd4_replay_cache_entry(resp, seq);
  1805. cstate->status = nfserr_replay_cache;
  1806. goto out;
  1807. }
  1808. if (status)
  1809. goto out;
  1810. nfsd4_sequence_check_conn(conn, session);
  1811. conn = NULL;
  1812. /* Success! bump slot seqid */
  1813. slot->sl_seqid = seq->seqid;
  1814. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1815. if (seq->cachethis)
  1816. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1817. else
  1818. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1819. cstate->slot = slot;
  1820. cstate->session = session;
  1821. out:
  1822. /* Hold a session reference until done processing the compound. */
  1823. if (cstate->session) {
  1824. struct nfs4_client *clp = session->se_client;
  1825. nfsd4_get_session(cstate->session);
  1826. atomic_inc(&clp->cl_refcount);
  1827. switch (clp->cl_cb_state) {
  1828. case NFSD4_CB_DOWN:
  1829. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1830. break;
  1831. case NFSD4_CB_FAULT:
  1832. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1833. break;
  1834. default:
  1835. seq->status_flags = 0;
  1836. }
  1837. }
  1838. kfree(conn);
  1839. spin_unlock(&nn->client_lock);
  1840. dprintk("%s: return %d\n", __func__, ntohl(status));
  1841. return status;
  1842. }
  1843. __be32
  1844. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1845. {
  1846. struct nfs4_client *conf, *unconf, *clp;
  1847. __be32 status = 0;
  1848. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1849. nfs4_lock_state();
  1850. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  1851. conf = find_confirmed_client(&dc->clientid, true, nn);
  1852. if (conf) {
  1853. clp = conf;
  1854. if (!is_client_expired(conf) && client_has_state(conf)) {
  1855. status = nfserr_clientid_busy;
  1856. goto out;
  1857. }
  1858. /* rfc5661 18.50.3 */
  1859. if (cstate->session && conf == cstate->session->se_client) {
  1860. status = nfserr_clientid_busy;
  1861. goto out;
  1862. }
  1863. } else if (unconf)
  1864. clp = unconf;
  1865. else {
  1866. status = nfserr_stale_clientid;
  1867. goto out;
  1868. }
  1869. expire_client(clp);
  1870. out:
  1871. nfs4_unlock_state();
  1872. dprintk("%s return %d\n", __func__, ntohl(status));
  1873. return status;
  1874. }
  1875. __be32
  1876. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1877. {
  1878. __be32 status = 0;
  1879. if (rc->rca_one_fs) {
  1880. if (!cstate->current_fh.fh_dentry)
  1881. return nfserr_nofilehandle;
  1882. /*
  1883. * We don't take advantage of the rca_one_fs case.
  1884. * That's OK, it's optional, we can safely ignore it.
  1885. */
  1886. return nfs_ok;
  1887. }
  1888. nfs4_lock_state();
  1889. status = nfserr_complete_already;
  1890. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1891. &cstate->session->se_client->cl_flags))
  1892. goto out;
  1893. status = nfserr_stale_clientid;
  1894. if (is_client_expired(cstate->session->se_client))
  1895. /*
  1896. * The following error isn't really legal.
  1897. * But we only get here if the client just explicitly
  1898. * destroyed the client. Surely it no longer cares what
  1899. * error it gets back on an operation for the dead
  1900. * client.
  1901. */
  1902. goto out;
  1903. status = nfs_ok;
  1904. nfsd4_client_record_create(cstate->session->se_client);
  1905. out:
  1906. nfs4_unlock_state();
  1907. return status;
  1908. }
  1909. __be32
  1910. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1911. struct nfsd4_setclientid *setclid)
  1912. {
  1913. struct xdr_netobj clname = setclid->se_name;
  1914. nfs4_verifier clverifier = setclid->se_verf;
  1915. struct nfs4_client *conf, *unconf, *new;
  1916. __be32 status;
  1917. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1918. /* Cases below refer to rfc 3530 section 14.2.33: */
  1919. nfs4_lock_state();
  1920. conf = find_confirmed_client_by_name(&clname, nn);
  1921. if (conf) {
  1922. /* case 0: */
  1923. status = nfserr_clid_inuse;
  1924. if (clp_used_exchangeid(conf))
  1925. goto out;
  1926. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1927. char addr_str[INET6_ADDRSTRLEN];
  1928. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1929. sizeof(addr_str));
  1930. dprintk("NFSD: setclientid: string in use by client "
  1931. "at %s\n", addr_str);
  1932. goto out;
  1933. }
  1934. }
  1935. unconf = find_unconfirmed_client_by_name(&clname, nn);
  1936. if (unconf)
  1937. expire_client(unconf);
  1938. status = nfserr_jukebox;
  1939. new = create_client(clname, rqstp, &clverifier);
  1940. if (new == NULL)
  1941. goto out;
  1942. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  1943. /* case 1: probable callback update */
  1944. copy_clid(new, conf);
  1945. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  1946. gen_clid(new, nn);
  1947. new->cl_minorversion = 0;
  1948. gen_callback(new, setclid, rqstp);
  1949. add_to_unconfirmed(new);
  1950. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  1951. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  1952. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  1953. status = nfs_ok;
  1954. out:
  1955. nfs4_unlock_state();
  1956. return status;
  1957. }
  1958. __be32
  1959. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  1960. struct nfsd4_compound_state *cstate,
  1961. struct nfsd4_setclientid_confirm *setclientid_confirm)
  1962. {
  1963. struct nfs4_client *conf, *unconf;
  1964. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  1965. clientid_t * clid = &setclientid_confirm->sc_clientid;
  1966. __be32 status;
  1967. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1968. if (STALE_CLIENTID(clid, nn))
  1969. return nfserr_stale_clientid;
  1970. nfs4_lock_state();
  1971. conf = find_confirmed_client(clid, false, nn);
  1972. unconf = find_unconfirmed_client(clid, false, nn);
  1973. /*
  1974. * We try hard to give out unique clientid's, so if we get an
  1975. * attempt to confirm the same clientid with a different cred,
  1976. * there's a bug somewhere. Let's charitably assume it's our
  1977. * bug.
  1978. */
  1979. status = nfserr_serverfault;
  1980. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  1981. goto out;
  1982. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  1983. goto out;
  1984. /* cases below refer to rfc 3530 section 14.2.34: */
  1985. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  1986. if (conf && !unconf) /* case 2: probable retransmit */
  1987. status = nfs_ok;
  1988. else /* case 4: client hasn't noticed we rebooted yet? */
  1989. status = nfserr_stale_clientid;
  1990. goto out;
  1991. }
  1992. status = nfs_ok;
  1993. if (conf) { /* case 1: callback update */
  1994. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  1995. nfsd4_probe_callback(conf);
  1996. expire_client(unconf);
  1997. } else { /* case 3: normal case; new or rebooted client */
  1998. conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1999. if (conf)
  2000. expire_client(conf);
  2001. move_to_confirmed(unconf);
  2002. nfsd4_probe_callback(unconf);
  2003. }
  2004. out:
  2005. nfs4_unlock_state();
  2006. return status;
  2007. }
  2008. static struct nfs4_file *nfsd4_alloc_file(void)
  2009. {
  2010. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2011. }
  2012. /* OPEN Share state helper functions */
  2013. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  2014. {
  2015. unsigned int hashval = file_hashval(ino);
  2016. atomic_set(&fp->fi_ref, 1);
  2017. INIT_LIST_HEAD(&fp->fi_hash);
  2018. INIT_LIST_HEAD(&fp->fi_stateids);
  2019. INIT_LIST_HEAD(&fp->fi_delegations);
  2020. fp->fi_inode = igrab(ino);
  2021. fp->fi_had_conflict = false;
  2022. fp->fi_lease = NULL;
  2023. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2024. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2025. spin_lock(&recall_lock);
  2026. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  2027. spin_unlock(&recall_lock);
  2028. }
  2029. static void
  2030. nfsd4_free_slab(struct kmem_cache **slab)
  2031. {
  2032. if (*slab == NULL)
  2033. return;
  2034. kmem_cache_destroy(*slab);
  2035. *slab = NULL;
  2036. }
  2037. void
  2038. nfsd4_free_slabs(void)
  2039. {
  2040. nfsd4_free_slab(&openowner_slab);
  2041. nfsd4_free_slab(&lockowner_slab);
  2042. nfsd4_free_slab(&file_slab);
  2043. nfsd4_free_slab(&stateid_slab);
  2044. nfsd4_free_slab(&deleg_slab);
  2045. }
  2046. int
  2047. nfsd4_init_slabs(void)
  2048. {
  2049. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2050. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2051. if (openowner_slab == NULL)
  2052. goto out_nomem;
  2053. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2054. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2055. if (lockowner_slab == NULL)
  2056. goto out_nomem;
  2057. file_slab = kmem_cache_create("nfsd4_files",
  2058. sizeof(struct nfs4_file), 0, 0, NULL);
  2059. if (file_slab == NULL)
  2060. goto out_nomem;
  2061. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2062. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2063. if (stateid_slab == NULL)
  2064. goto out_nomem;
  2065. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2066. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2067. if (deleg_slab == NULL)
  2068. goto out_nomem;
  2069. return 0;
  2070. out_nomem:
  2071. nfsd4_free_slabs();
  2072. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2073. return -ENOMEM;
  2074. }
  2075. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2076. {
  2077. kfree(oo->oo_owner.so_owner.data);
  2078. kmem_cache_free(openowner_slab, oo);
  2079. }
  2080. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2081. {
  2082. kfree(lo->lo_owner.so_owner.data);
  2083. kmem_cache_free(lockowner_slab, lo);
  2084. }
  2085. static void init_nfs4_replay(struct nfs4_replay *rp)
  2086. {
  2087. rp->rp_status = nfserr_serverfault;
  2088. rp->rp_buflen = 0;
  2089. rp->rp_buf = rp->rp_ibuf;
  2090. }
  2091. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2092. {
  2093. struct nfs4_stateowner *sop;
  2094. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2095. if (!sop)
  2096. return NULL;
  2097. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2098. if (!sop->so_owner.data) {
  2099. kmem_cache_free(slab, sop);
  2100. return NULL;
  2101. }
  2102. sop->so_owner.len = owner->len;
  2103. INIT_LIST_HEAD(&sop->so_stateids);
  2104. sop->so_client = clp;
  2105. init_nfs4_replay(&sop->so_replay);
  2106. return sop;
  2107. }
  2108. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2109. {
  2110. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2111. list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  2112. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2113. }
  2114. static struct nfs4_openowner *
  2115. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2116. struct nfs4_openowner *oo;
  2117. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2118. if (!oo)
  2119. return NULL;
  2120. oo->oo_owner.so_is_open_owner = 1;
  2121. oo->oo_owner.so_seqid = open->op_seqid;
  2122. oo->oo_flags = NFS4_OO_NEW;
  2123. oo->oo_time = 0;
  2124. oo->oo_last_closed_stid = NULL;
  2125. INIT_LIST_HEAD(&oo->oo_close_lru);
  2126. hash_openowner(oo, clp, strhashval);
  2127. return oo;
  2128. }
  2129. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2130. struct nfs4_openowner *oo = open->op_openowner;
  2131. struct nfs4_client *clp = oo->oo_owner.so_client;
  2132. init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
  2133. INIT_LIST_HEAD(&stp->st_lockowners);
  2134. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2135. list_add(&stp->st_perfile, &fp->fi_stateids);
  2136. stp->st_stateowner = &oo->oo_owner;
  2137. get_nfs4_file(fp);
  2138. stp->st_file = fp;
  2139. stp->st_access_bmap = 0;
  2140. stp->st_deny_bmap = 0;
  2141. set_access(open->op_share_access, stp);
  2142. set_deny(open->op_share_deny, stp);
  2143. stp->st_openstp = NULL;
  2144. }
  2145. static void
  2146. move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
  2147. {
  2148. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2149. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2150. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2151. oo->oo_time = get_seconds();
  2152. }
  2153. static int
  2154. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2155. clientid_t *clid)
  2156. {
  2157. return (sop->so_owner.len == owner->len) &&
  2158. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2159. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2160. }
  2161. static struct nfs4_openowner *
  2162. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  2163. bool sessions, struct nfsd_net *nn)
  2164. {
  2165. struct nfs4_stateowner *so;
  2166. struct nfs4_openowner *oo;
  2167. struct nfs4_client *clp;
  2168. list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  2169. if (!so->so_is_open_owner)
  2170. continue;
  2171. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2172. oo = openowner(so);
  2173. clp = oo->oo_owner.so_client;
  2174. if ((bool)clp->cl_minorversion != sessions)
  2175. return NULL;
  2176. renew_client(oo->oo_owner.so_client);
  2177. return oo;
  2178. }
  2179. }
  2180. return NULL;
  2181. }
  2182. /* search file_hashtbl[] for file */
  2183. static struct nfs4_file *
  2184. find_file(struct inode *ino)
  2185. {
  2186. unsigned int hashval = file_hashval(ino);
  2187. struct nfs4_file *fp;
  2188. spin_lock(&recall_lock);
  2189. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2190. if (fp->fi_inode == ino) {
  2191. get_nfs4_file(fp);
  2192. spin_unlock(&recall_lock);
  2193. return fp;
  2194. }
  2195. }
  2196. spin_unlock(&recall_lock);
  2197. return NULL;
  2198. }
  2199. /*
  2200. * Called to check deny when READ with all zero stateid or
  2201. * WRITE with all zero or all one stateid
  2202. */
  2203. static __be32
  2204. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2205. {
  2206. struct inode *ino = current_fh->fh_dentry->d_inode;
  2207. struct nfs4_file *fp;
  2208. struct nfs4_ol_stateid *stp;
  2209. __be32 ret;
  2210. dprintk("NFSD: nfs4_share_conflict\n");
  2211. fp = find_file(ino);
  2212. if (!fp)
  2213. return nfs_ok;
  2214. ret = nfserr_locked;
  2215. /* Search for conflicting share reservations */
  2216. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2217. if (test_deny(deny_type, stp) ||
  2218. test_deny(NFS4_SHARE_DENY_BOTH, stp))
  2219. goto out;
  2220. }
  2221. ret = nfs_ok;
  2222. out:
  2223. put_nfs4_file(fp);
  2224. return ret;
  2225. }
  2226. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2227. {
  2228. /* We're assuming the state code never drops its reference
  2229. * without first removing the lease. Since we're in this lease
  2230. * callback (and since the lease code is serialized by the kernel
  2231. * lock) we know the server hasn't removed the lease yet, we know
  2232. * it's safe to take a reference: */
  2233. atomic_inc(&dp->dl_count);
  2234. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  2235. /* only place dl_time is set. protected by lock_flocks*/
  2236. dp->dl_time = get_seconds();
  2237. nfsd4_cb_recall(dp);
  2238. }
  2239. /* Called from break_lease() with lock_flocks() held. */
  2240. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2241. {
  2242. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2243. struct nfs4_delegation *dp;
  2244. if (!fp) {
  2245. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2246. return;
  2247. }
  2248. if (fp->fi_had_conflict) {
  2249. WARN(1, "duplicate break on %p\n", fp);
  2250. return;
  2251. }
  2252. /*
  2253. * We don't want the locks code to timeout the lease for us;
  2254. * we'll remove it ourself if a delegation isn't returned
  2255. * in time:
  2256. */
  2257. fl->fl_break_time = 0;
  2258. spin_lock(&recall_lock);
  2259. fp->fi_had_conflict = true;
  2260. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2261. nfsd_break_one_deleg(dp);
  2262. spin_unlock(&recall_lock);
  2263. }
  2264. static
  2265. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2266. {
  2267. if (arg & F_UNLCK)
  2268. return lease_modify(onlist, arg);
  2269. else
  2270. return -EAGAIN;
  2271. }
  2272. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2273. .lm_break = nfsd_break_deleg_cb,
  2274. .lm_change = nfsd_change_deleg_cb,
  2275. };
  2276. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2277. {
  2278. if (nfsd4_has_session(cstate))
  2279. return nfs_ok;
  2280. if (seqid == so->so_seqid - 1)
  2281. return nfserr_replay_me;
  2282. if (seqid == so->so_seqid)
  2283. return nfs_ok;
  2284. return nfserr_bad_seqid;
  2285. }
  2286. __be32
  2287. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2288. struct nfsd4_open *open, struct nfsd_net *nn)
  2289. {
  2290. clientid_t *clientid = &open->op_clientid;
  2291. struct nfs4_client *clp = NULL;
  2292. unsigned int strhashval;
  2293. struct nfs4_openowner *oo = NULL;
  2294. __be32 status;
  2295. if (STALE_CLIENTID(&open->op_clientid, nn))
  2296. return nfserr_stale_clientid;
  2297. /*
  2298. * In case we need it later, after we've already created the
  2299. * file and don't want to risk a further failure:
  2300. */
  2301. open->op_file = nfsd4_alloc_file();
  2302. if (open->op_file == NULL)
  2303. return nfserr_jukebox;
  2304. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2305. oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
  2306. open->op_openowner = oo;
  2307. if (!oo) {
  2308. clp = find_confirmed_client(clientid, cstate->minorversion,
  2309. nn);
  2310. if (clp == NULL)
  2311. return nfserr_expired;
  2312. goto new_owner;
  2313. }
  2314. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2315. /* Replace unconfirmed owners without checking for replay. */
  2316. clp = oo->oo_owner.so_client;
  2317. release_openowner(oo);
  2318. open->op_openowner = NULL;
  2319. goto new_owner;
  2320. }
  2321. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2322. if (status)
  2323. return status;
  2324. clp = oo->oo_owner.so_client;
  2325. goto alloc_stateid;
  2326. new_owner:
  2327. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2328. if (oo == NULL)
  2329. return nfserr_jukebox;
  2330. open->op_openowner = oo;
  2331. alloc_stateid:
  2332. open->op_stp = nfs4_alloc_stateid(clp);
  2333. if (!open->op_stp)
  2334. return nfserr_jukebox;
  2335. return nfs_ok;
  2336. }
  2337. static inline __be32
  2338. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2339. {
  2340. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2341. return nfserr_openmode;
  2342. else
  2343. return nfs_ok;
  2344. }
  2345. static int share_access_to_flags(u32 share_access)
  2346. {
  2347. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2348. }
  2349. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2350. {
  2351. struct nfs4_stid *ret;
  2352. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2353. if (!ret)
  2354. return NULL;
  2355. return delegstateid(ret);
  2356. }
  2357. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2358. {
  2359. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2360. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2361. }
  2362. static __be32
  2363. nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
  2364. struct nfs4_delegation **dp)
  2365. {
  2366. int flags;
  2367. __be32 status = nfserr_bad_stateid;
  2368. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2369. if (*dp == NULL)
  2370. goto out;
  2371. flags = share_access_to_flags(open->op_share_access);
  2372. status = nfs4_check_delegmode(*dp, flags);
  2373. if (status)
  2374. *dp = NULL;
  2375. out:
  2376. if (!nfsd4_is_deleg_cur(open))
  2377. return nfs_ok;
  2378. if (status)
  2379. return status;
  2380. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2381. return nfs_ok;
  2382. }
  2383. static __be32
  2384. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2385. {
  2386. struct nfs4_ol_stateid *local;
  2387. struct nfs4_openowner *oo = open->op_openowner;
  2388. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2389. /* ignore lock owners */
  2390. if (local->st_stateowner->so_is_open_owner == 0)
  2391. continue;
  2392. /* remember if we have seen this open owner */
  2393. if (local->st_stateowner == &oo->oo_owner)
  2394. *stpp = local;
  2395. /* check for conflicting share reservations */
  2396. if (!test_share(local, open))
  2397. return nfserr_share_denied;
  2398. }
  2399. return nfs_ok;
  2400. }
  2401. static inline int nfs4_access_to_access(u32 nfs4_access)
  2402. {
  2403. int flags = 0;
  2404. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2405. flags |= NFSD_MAY_READ;
  2406. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2407. flags |= NFSD_MAY_WRITE;
  2408. return flags;
  2409. }
  2410. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2411. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2412. {
  2413. __be32 status;
  2414. int oflag = nfs4_access_to_omode(open->op_share_access);
  2415. int access = nfs4_access_to_access(open->op_share_access);
  2416. if (!fp->fi_fds[oflag]) {
  2417. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2418. &fp->fi_fds[oflag]);
  2419. if (status)
  2420. return status;
  2421. }
  2422. nfs4_file_get_access(fp, oflag);
  2423. return nfs_ok;
  2424. }
  2425. static inline __be32
  2426. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2427. struct nfsd4_open *open)
  2428. {
  2429. struct iattr iattr = {
  2430. .ia_valid = ATTR_SIZE,
  2431. .ia_size = 0,
  2432. };
  2433. if (!open->op_truncate)
  2434. return 0;
  2435. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2436. return nfserr_inval;
  2437. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2438. }
  2439. static __be32
  2440. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
  2441. {
  2442. u32 op_share_access = open->op_share_access;
  2443. bool new_access;
  2444. __be32 status;
  2445. new_access = !test_access(op_share_access, stp);
  2446. if (new_access) {
  2447. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2448. if (status)
  2449. return status;
  2450. }
  2451. status = nfsd4_truncate(rqstp, cur_fh, open);
  2452. if (status) {
  2453. if (new_access) {
  2454. int oflag = nfs4_access_to_omode(op_share_access);
  2455. nfs4_file_put_access(fp, oflag);
  2456. }
  2457. return status;
  2458. }
  2459. /* remember the open */
  2460. set_access(op_share_access, stp);
  2461. set_deny(open->op_share_deny, stp);
  2462. return nfs_ok;
  2463. }
  2464. static void
  2465. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2466. {
  2467. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2468. }
  2469. /* Should we give out recallable state?: */
  2470. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2471. {
  2472. if (clp->cl_cb_state == NFSD4_CB_UP)
  2473. return true;
  2474. /*
  2475. * In the sessions case, since we don't have to establish a
  2476. * separate connection for callbacks, we assume it's OK
  2477. * until we hear otherwise:
  2478. */
  2479. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2480. }
  2481. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2482. {
  2483. struct file_lock *fl;
  2484. fl = locks_alloc_lock();
  2485. if (!fl)
  2486. return NULL;
  2487. locks_init_lock(fl);
  2488. fl->fl_lmops = &nfsd_lease_mng_ops;
  2489. fl->fl_flags = FL_LEASE;
  2490. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2491. fl->fl_end = OFFSET_MAX;
  2492. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2493. fl->fl_pid = current->tgid;
  2494. return fl;
  2495. }
  2496. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2497. {
  2498. struct nfs4_file *fp = dp->dl_file;
  2499. struct file_lock *fl;
  2500. int status;
  2501. fl = nfs4_alloc_init_lease(dp, flag);
  2502. if (!fl)
  2503. return -ENOMEM;
  2504. fl->fl_file = find_readable_file(fp);
  2505. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2506. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2507. if (status) {
  2508. list_del_init(&dp->dl_perclnt);
  2509. locks_free_lock(fl);
  2510. return -ENOMEM;
  2511. }
  2512. fp->fi_lease = fl;
  2513. fp->fi_deleg_file = get_file(fl->fl_file);
  2514. atomic_set(&fp->fi_delegees, 1);
  2515. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2516. return 0;
  2517. }
  2518. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2519. {
  2520. struct nfs4_file *fp = dp->dl_file;
  2521. if (!fp->fi_lease)
  2522. return nfs4_setlease(dp, flag);
  2523. spin_lock(&recall_lock);
  2524. if (fp->fi_had_conflict) {
  2525. spin_unlock(&recall_lock);
  2526. return -EAGAIN;
  2527. }
  2528. atomic_inc(&fp->fi_delegees);
  2529. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2530. spin_unlock(&recall_lock);
  2531. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2532. return 0;
  2533. }
  2534. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2535. {
  2536. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2537. if (status == -EAGAIN)
  2538. open->op_why_no_deleg = WND4_CONTENTION;
  2539. else {
  2540. open->op_why_no_deleg = WND4_RESOURCE;
  2541. switch (open->op_deleg_want) {
  2542. case NFS4_SHARE_WANT_READ_DELEG:
  2543. case NFS4_SHARE_WANT_WRITE_DELEG:
  2544. case NFS4_SHARE_WANT_ANY_DELEG:
  2545. break;
  2546. case NFS4_SHARE_WANT_CANCEL:
  2547. open->op_why_no_deleg = WND4_CANCELLED;
  2548. break;
  2549. case NFS4_SHARE_WANT_NO_DELEG:
  2550. WARN_ON_ONCE(1);
  2551. }
  2552. }
  2553. }
  2554. /*
  2555. * Attempt to hand out a delegation.
  2556. */
  2557. static void
  2558. nfs4_open_delegation(struct net *net, struct svc_fh *fh,
  2559. struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2560. {
  2561. struct nfs4_delegation *dp;
  2562. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2563. int cb_up;
  2564. int status = 0, flag = 0;
  2565. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2566. flag = NFS4_OPEN_DELEGATE_NONE;
  2567. open->op_recall = 0;
  2568. switch (open->op_claim_type) {
  2569. case NFS4_OPEN_CLAIM_PREVIOUS:
  2570. if (!cb_up)
  2571. open->op_recall = 1;
  2572. flag = open->op_delegate_type;
  2573. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2574. goto out;
  2575. break;
  2576. case NFS4_OPEN_CLAIM_NULL:
  2577. /* Let's not give out any delegations till everyone's
  2578. * had the chance to reclaim theirs.... */
  2579. if (locks_in_grace(net))
  2580. goto out;
  2581. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2582. goto out;
  2583. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2584. flag = NFS4_OPEN_DELEGATE_WRITE;
  2585. else
  2586. flag = NFS4_OPEN_DELEGATE_READ;
  2587. break;
  2588. default:
  2589. goto out;
  2590. }
  2591. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2592. if (dp == NULL)
  2593. goto out_no_deleg;
  2594. status = nfs4_set_delegation(dp, flag);
  2595. if (status)
  2596. goto out_free;
  2597. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2598. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2599. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2600. out:
  2601. open->op_delegate_type = flag;
  2602. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2603. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2604. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2605. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2606. /* 4.1 client asking for a delegation? */
  2607. if (open->op_deleg_want)
  2608. nfsd4_open_deleg_none_ext(open, status);
  2609. }
  2610. return;
  2611. out_free:
  2612. nfs4_put_delegation(dp);
  2613. out_no_deleg:
  2614. flag = NFS4_OPEN_DELEGATE_NONE;
  2615. goto out;
  2616. }
  2617. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2618. struct nfs4_delegation *dp)
  2619. {
  2620. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2621. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2622. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2623. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2624. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2625. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2626. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2627. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2628. }
  2629. /* Otherwise the client must be confused wanting a delegation
  2630. * it already has, therefore we don't return
  2631. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2632. */
  2633. }
  2634. /*
  2635. * called with nfs4_lock_state() held.
  2636. */
  2637. __be32
  2638. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2639. {
  2640. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2641. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2642. struct nfs4_file *fp = NULL;
  2643. struct inode *ino = current_fh->fh_dentry->d_inode;
  2644. struct nfs4_ol_stateid *stp = NULL;
  2645. struct nfs4_delegation *dp = NULL;
  2646. __be32 status;
  2647. /*
  2648. * Lookup file; if found, lookup stateid and check open request,
  2649. * and check for delegations in the process of being recalled.
  2650. * If not found, create the nfs4_file struct
  2651. */
  2652. fp = find_file(ino);
  2653. if (fp) {
  2654. if ((status = nfs4_check_open(fp, open, &stp)))
  2655. goto out;
  2656. status = nfs4_check_deleg(cl, fp, open, &dp);
  2657. if (status)
  2658. goto out;
  2659. } else {
  2660. status = nfserr_bad_stateid;
  2661. if (nfsd4_is_deleg_cur(open))
  2662. goto out;
  2663. status = nfserr_jukebox;
  2664. fp = open->op_file;
  2665. open->op_file = NULL;
  2666. nfsd4_init_file(fp, ino);
  2667. }
  2668. /*
  2669. * OPEN the file, or upgrade an existing OPEN.
  2670. * If truncate fails, the OPEN fails.
  2671. */
  2672. if (stp) {
  2673. /* Stateid was found, this is an OPEN upgrade */
  2674. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2675. if (status)
  2676. goto out;
  2677. } else {
  2678. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2679. if (status)
  2680. goto out;
  2681. status = nfsd4_truncate(rqstp, current_fh, open);
  2682. if (status)
  2683. goto out;
  2684. stp = open->op_stp;
  2685. open->op_stp = NULL;
  2686. init_open_stateid(stp, fp, open);
  2687. }
  2688. update_stateid(&stp->st_stid.sc_stateid);
  2689. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2690. if (nfsd4_has_session(&resp->cstate)) {
  2691. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2692. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2693. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2694. open->op_why_no_deleg = WND4_NOT_WANTED;
  2695. goto nodeleg;
  2696. }
  2697. }
  2698. /*
  2699. * Attempt to hand out a delegation. No error return, because the
  2700. * OPEN succeeds even if we fail.
  2701. */
  2702. nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
  2703. nodeleg:
  2704. status = nfs_ok;
  2705. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2706. STATEID_VAL(&stp->st_stid.sc_stateid));
  2707. out:
  2708. /* 4.1 client trying to upgrade/downgrade delegation? */
  2709. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2710. open->op_deleg_want)
  2711. nfsd4_deleg_xgrade_none_ext(open, dp);
  2712. if (fp)
  2713. put_nfs4_file(fp);
  2714. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2715. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2716. /*
  2717. * To finish the open response, we just need to set the rflags.
  2718. */
  2719. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2720. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2721. !nfsd4_has_session(&resp->cstate))
  2722. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2723. return status;
  2724. }
  2725. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2726. {
  2727. if (open->op_openowner) {
  2728. struct nfs4_openowner *oo = open->op_openowner;
  2729. if (!list_empty(&oo->oo_owner.so_stateids))
  2730. list_del_init(&oo->oo_close_lru);
  2731. if (oo->oo_flags & NFS4_OO_NEW) {
  2732. if (status) {
  2733. release_openowner(oo);
  2734. open->op_openowner = NULL;
  2735. } else
  2736. oo->oo_flags &= ~NFS4_OO_NEW;
  2737. }
  2738. }
  2739. if (open->op_file)
  2740. nfsd4_free_file(open->op_file);
  2741. if (open->op_stp)
  2742. free_generic_stateid(open->op_stp);
  2743. }
  2744. __be32
  2745. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2746. clientid_t *clid)
  2747. {
  2748. struct nfs4_client *clp;
  2749. __be32 status;
  2750. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2751. nfs4_lock_state();
  2752. dprintk("process_renew(%08x/%08x): starting\n",
  2753. clid->cl_boot, clid->cl_id);
  2754. status = nfserr_stale_clientid;
  2755. if (STALE_CLIENTID(clid, nn))
  2756. goto out;
  2757. clp = find_confirmed_client(clid, cstate->minorversion, nn);
  2758. status = nfserr_expired;
  2759. if (clp == NULL) {
  2760. /* We assume the client took too long to RENEW. */
  2761. dprintk("nfsd4_renew: clientid not found!\n");
  2762. goto out;
  2763. }
  2764. status = nfserr_cb_path_down;
  2765. if (!list_empty(&clp->cl_delegations)
  2766. && clp->cl_cb_state != NFSD4_CB_UP)
  2767. goto out;
  2768. status = nfs_ok;
  2769. out:
  2770. nfs4_unlock_state();
  2771. return status;
  2772. }
  2773. static void
  2774. nfsd4_end_grace(struct nfsd_net *nn)
  2775. {
  2776. /* do nothing if grace period already ended */
  2777. if (nn->grace_ended)
  2778. return;
  2779. dprintk("NFSD: end of grace period\n");
  2780. nn->grace_ended = true;
  2781. nfsd4_record_grace_done(nn, nn->boot_time);
  2782. locks_end_grace(&nn->nfsd4_manager);
  2783. /*
  2784. * Now that every NFSv4 client has had the chance to recover and
  2785. * to see the (possibly new, possibly shorter) lease time, we
  2786. * can safely set the next grace time to the current lease time:
  2787. */
  2788. nfsd4_grace = nn->nfsd4_lease;
  2789. }
  2790. static time_t
  2791. nfs4_laundromat(struct nfsd_net *nn)
  2792. {
  2793. struct nfs4_client *clp;
  2794. struct nfs4_openowner *oo;
  2795. struct nfs4_delegation *dp;
  2796. struct list_head *pos, *next, reaplist;
  2797. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  2798. time_t t, clientid_val = nn->nfsd4_lease;
  2799. time_t u, test_val = nn->nfsd4_lease;
  2800. nfs4_lock_state();
  2801. dprintk("NFSD: laundromat service - starting\n");
  2802. nfsd4_end_grace(nn);
  2803. INIT_LIST_HEAD(&reaplist);
  2804. spin_lock(&nn->client_lock);
  2805. list_for_each_safe(pos, next, &nn->client_lru) {
  2806. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2807. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2808. t = clp->cl_time - cutoff;
  2809. if (clientid_val > t)
  2810. clientid_val = t;
  2811. break;
  2812. }
  2813. if (atomic_read(&clp->cl_refcount)) {
  2814. dprintk("NFSD: client in use (clientid %08x)\n",
  2815. clp->cl_clientid.cl_id);
  2816. continue;
  2817. }
  2818. unhash_client_locked(clp);
  2819. list_add(&clp->cl_lru, &reaplist);
  2820. }
  2821. spin_unlock(&nn->client_lock);
  2822. list_for_each_safe(pos, next, &reaplist) {
  2823. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2824. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2825. clp->cl_clientid.cl_id);
  2826. expire_client(clp);
  2827. }
  2828. spin_lock(&recall_lock);
  2829. list_for_each_safe(pos, next, &del_recall_lru) {
  2830. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2831. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  2832. continue;
  2833. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2834. u = dp->dl_time - cutoff;
  2835. if (test_val > u)
  2836. test_val = u;
  2837. break;
  2838. }
  2839. list_move(&dp->dl_recall_lru, &reaplist);
  2840. }
  2841. spin_unlock(&recall_lock);
  2842. list_for_each_safe(pos, next, &reaplist) {
  2843. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2844. unhash_delegation(dp);
  2845. }
  2846. test_val = nn->nfsd4_lease;
  2847. list_for_each_safe(pos, next, &nn->close_lru) {
  2848. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2849. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2850. u = oo->oo_time - cutoff;
  2851. if (test_val > u)
  2852. test_val = u;
  2853. break;
  2854. }
  2855. release_openowner(oo);
  2856. }
  2857. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2858. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2859. nfs4_unlock_state();
  2860. return clientid_val;
  2861. }
  2862. static struct workqueue_struct *laundry_wq;
  2863. static void laundromat_main(struct work_struct *);
  2864. static void
  2865. laundromat_main(struct work_struct *laundry)
  2866. {
  2867. time_t t;
  2868. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  2869. work);
  2870. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  2871. laundromat_work);
  2872. t = nfs4_laundromat(nn);
  2873. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2874. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  2875. }
  2876. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2877. {
  2878. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2879. return nfserr_bad_stateid;
  2880. return nfs_ok;
  2881. }
  2882. static int
  2883. STALE_STATEID(stateid_t *stateid, struct nfsd_net *nn)
  2884. {
  2885. if (stateid->si_opaque.so_clid.cl_boot == nn->boot_time)
  2886. return 0;
  2887. dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
  2888. STATEID_VAL(stateid));
  2889. return 1;
  2890. }
  2891. static inline int
  2892. access_permit_read(struct nfs4_ol_stateid *stp)
  2893. {
  2894. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  2895. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  2896. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  2897. }
  2898. static inline int
  2899. access_permit_write(struct nfs4_ol_stateid *stp)
  2900. {
  2901. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  2902. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  2903. }
  2904. static
  2905. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2906. {
  2907. __be32 status = nfserr_openmode;
  2908. /* For lock stateid's, we test the parent open, not the lock: */
  2909. if (stp->st_openstp)
  2910. stp = stp->st_openstp;
  2911. if ((flags & WR_STATE) && !access_permit_write(stp))
  2912. goto out;
  2913. if ((flags & RD_STATE) && !access_permit_read(stp))
  2914. goto out;
  2915. status = nfs_ok;
  2916. out:
  2917. return status;
  2918. }
  2919. static inline __be32
  2920. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  2921. {
  2922. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2923. return nfs_ok;
  2924. else if (locks_in_grace(net)) {
  2925. /* Answer in remaining cases depends on existence of
  2926. * conflicting state; so we must wait out the grace period. */
  2927. return nfserr_grace;
  2928. } else if (flags & WR_STATE)
  2929. return nfs4_share_conflict(current_fh,
  2930. NFS4_SHARE_DENY_WRITE);
  2931. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2932. return nfs4_share_conflict(current_fh,
  2933. NFS4_SHARE_DENY_READ);
  2934. }
  2935. /*
  2936. * Allow READ/WRITE during grace period on recovered state only for files
  2937. * that are not able to provide mandatory locking.
  2938. */
  2939. static inline int
  2940. grace_disallows_io(struct net *net, struct inode *inode)
  2941. {
  2942. return locks_in_grace(net) && mandatory_lock(inode);
  2943. }
  2944. /* Returns true iff a is later than b: */
  2945. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  2946. {
  2947. return (s32)a->si_generation - (s32)b->si_generation > 0;
  2948. }
  2949. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  2950. {
  2951. /*
  2952. * When sessions are used the stateid generation number is ignored
  2953. * when it is zero.
  2954. */
  2955. if (has_session && in->si_generation == 0)
  2956. return nfs_ok;
  2957. if (in->si_generation == ref->si_generation)
  2958. return nfs_ok;
  2959. /* If the client sends us a stateid from the future, it's buggy: */
  2960. if (stateid_generation_after(in, ref))
  2961. return nfserr_bad_stateid;
  2962. /*
  2963. * However, we could see a stateid from the past, even from a
  2964. * non-buggy client. For example, if the client sends a lock
  2965. * while some IO is outstanding, the lock may bump si_generation
  2966. * while the IO is still in flight. The client could avoid that
  2967. * situation by waiting for responses on all the IO requests,
  2968. * but better performance may result in retrying IO that
  2969. * receives an old_stateid error if requests are rarely
  2970. * reordered in flight:
  2971. */
  2972. return nfserr_old_stateid;
  2973. }
  2974. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  2975. {
  2976. struct nfs4_stid *s;
  2977. struct nfs4_ol_stateid *ols;
  2978. __be32 status;
  2979. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2980. return nfserr_bad_stateid;
  2981. /* Client debugging aid. */
  2982. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  2983. char addr_str[INET6_ADDRSTRLEN];
  2984. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  2985. sizeof(addr_str));
  2986. pr_warn_ratelimited("NFSD: client %s testing state ID "
  2987. "with incorrect client ID\n", addr_str);
  2988. return nfserr_bad_stateid;
  2989. }
  2990. s = find_stateid(cl, stateid);
  2991. if (!s)
  2992. return nfserr_bad_stateid;
  2993. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  2994. if (status)
  2995. return status;
  2996. if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
  2997. return nfs_ok;
  2998. ols = openlockstateid(s);
  2999. if (ols->st_stateowner->so_is_open_owner
  3000. && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3001. return nfserr_bad_stateid;
  3002. return nfs_ok;
  3003. }
  3004. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
  3005. struct nfs4_stid **s, bool sessions,
  3006. struct nfsd_net *nn)
  3007. {
  3008. struct nfs4_client *cl;
  3009. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3010. return nfserr_bad_stateid;
  3011. if (STALE_STATEID(stateid, nn))
  3012. return nfserr_stale_stateid;
  3013. cl = find_confirmed_client(&stateid->si_opaque.so_clid, sessions, nn);
  3014. if (!cl)
  3015. return nfserr_expired;
  3016. *s = find_stateid_by_type(cl, stateid, typemask);
  3017. if (!*s)
  3018. return nfserr_bad_stateid;
  3019. return nfs_ok;
  3020. }
  3021. /*
  3022. * Checks for stateid operations
  3023. */
  3024. __be32
  3025. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3026. stateid_t *stateid, int flags, struct file **filpp)
  3027. {
  3028. struct nfs4_stid *s;
  3029. struct nfs4_ol_stateid *stp = NULL;
  3030. struct nfs4_delegation *dp = NULL;
  3031. struct svc_fh *current_fh = &cstate->current_fh;
  3032. struct inode *ino = current_fh->fh_dentry->d_inode;
  3033. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3034. __be32 status;
  3035. if (filpp)
  3036. *filpp = NULL;
  3037. if (grace_disallows_io(net, ino))
  3038. return nfserr_grace;
  3039. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3040. return check_special_stateids(net, current_fh, stateid, flags);
  3041. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3042. &s, cstate->minorversion, nn);
  3043. if (status)
  3044. return status;
  3045. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3046. if (status)
  3047. goto out;
  3048. switch (s->sc_type) {
  3049. case NFS4_DELEG_STID:
  3050. dp = delegstateid(s);
  3051. status = nfs4_check_delegmode(dp, flags);
  3052. if (status)
  3053. goto out;
  3054. if (filpp) {
  3055. *filpp = dp->dl_file->fi_deleg_file;
  3056. if (!*filpp) {
  3057. WARN_ON_ONCE(1);
  3058. status = nfserr_serverfault;
  3059. goto out;
  3060. }
  3061. }
  3062. break;
  3063. case NFS4_OPEN_STID:
  3064. case NFS4_LOCK_STID:
  3065. stp = openlockstateid(s);
  3066. status = nfs4_check_fh(current_fh, stp);
  3067. if (status)
  3068. goto out;
  3069. if (stp->st_stateowner->so_is_open_owner
  3070. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3071. goto out;
  3072. status = nfs4_check_openmode(stp, flags);
  3073. if (status)
  3074. goto out;
  3075. if (filpp) {
  3076. if (flags & RD_STATE)
  3077. *filpp = find_readable_file(stp->st_file);
  3078. else
  3079. *filpp = find_writeable_file(stp->st_file);
  3080. }
  3081. break;
  3082. default:
  3083. return nfserr_bad_stateid;
  3084. }
  3085. status = nfs_ok;
  3086. out:
  3087. return status;
  3088. }
  3089. static __be32
  3090. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3091. {
  3092. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3093. return nfserr_locks_held;
  3094. release_lock_stateid(stp);
  3095. return nfs_ok;
  3096. }
  3097. /*
  3098. * Test if the stateid is valid
  3099. */
  3100. __be32
  3101. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3102. struct nfsd4_test_stateid *test_stateid)
  3103. {
  3104. struct nfsd4_test_stateid_id *stateid;
  3105. struct nfs4_client *cl = cstate->session->se_client;
  3106. nfs4_lock_state();
  3107. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3108. stateid->ts_id_status =
  3109. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3110. nfs4_unlock_state();
  3111. return nfs_ok;
  3112. }
  3113. __be32
  3114. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3115. struct nfsd4_free_stateid *free_stateid)
  3116. {
  3117. stateid_t *stateid = &free_stateid->fr_stateid;
  3118. struct nfs4_stid *s;
  3119. struct nfs4_client *cl = cstate->session->se_client;
  3120. __be32 ret = nfserr_bad_stateid;
  3121. nfs4_lock_state();
  3122. s = find_stateid(cl, stateid);
  3123. if (!s)
  3124. goto out;
  3125. switch (s->sc_type) {
  3126. case NFS4_DELEG_STID:
  3127. ret = nfserr_locks_held;
  3128. goto out;
  3129. case NFS4_OPEN_STID:
  3130. case NFS4_LOCK_STID:
  3131. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3132. if (ret)
  3133. goto out;
  3134. if (s->sc_type == NFS4_LOCK_STID)
  3135. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3136. else
  3137. ret = nfserr_locks_held;
  3138. break;
  3139. default:
  3140. ret = nfserr_bad_stateid;
  3141. }
  3142. out:
  3143. nfs4_unlock_state();
  3144. return ret;
  3145. }
  3146. static inline int
  3147. setlkflg (int type)
  3148. {
  3149. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3150. RD_STATE : WR_STATE;
  3151. }
  3152. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3153. {
  3154. struct svc_fh *current_fh = &cstate->current_fh;
  3155. struct nfs4_stateowner *sop = stp->st_stateowner;
  3156. __be32 status;
  3157. status = nfsd4_check_seqid(cstate, sop, seqid);
  3158. if (status)
  3159. return status;
  3160. if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
  3161. /*
  3162. * "Closed" stateid's exist *only* to return
  3163. * nfserr_replay_me from the previous step.
  3164. */
  3165. return nfserr_bad_stateid;
  3166. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3167. if (status)
  3168. return status;
  3169. return nfs4_check_fh(current_fh, stp);
  3170. }
  3171. /*
  3172. * Checks for sequence id mutating operations.
  3173. */
  3174. static __be32
  3175. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3176. stateid_t *stateid, char typemask,
  3177. struct nfs4_ol_stateid **stpp,
  3178. struct nfsd_net *nn)
  3179. {
  3180. __be32 status;
  3181. struct nfs4_stid *s;
  3182. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3183. seqid, STATEID_VAL(stateid));
  3184. *stpp = NULL;
  3185. status = nfsd4_lookup_stateid(stateid, typemask, &s,
  3186. cstate->minorversion, nn);
  3187. if (status)
  3188. return status;
  3189. *stpp = openlockstateid(s);
  3190. cstate->replay_owner = (*stpp)->st_stateowner;
  3191. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3192. }
  3193. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3194. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  3195. {
  3196. __be32 status;
  3197. struct nfs4_openowner *oo;
  3198. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3199. NFS4_OPEN_STID, stpp, nn);
  3200. if (status)
  3201. return status;
  3202. oo = openowner((*stpp)->st_stateowner);
  3203. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3204. return nfserr_bad_stateid;
  3205. return nfs_ok;
  3206. }
  3207. __be32
  3208. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3209. struct nfsd4_open_confirm *oc)
  3210. {
  3211. __be32 status;
  3212. struct nfs4_openowner *oo;
  3213. struct nfs4_ol_stateid *stp;
  3214. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3215. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3216. (int)cstate->current_fh.fh_dentry->d_name.len,
  3217. cstate->current_fh.fh_dentry->d_name.name);
  3218. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3219. if (status)
  3220. return status;
  3221. nfs4_lock_state();
  3222. status = nfs4_preprocess_seqid_op(cstate,
  3223. oc->oc_seqid, &oc->oc_req_stateid,
  3224. NFS4_OPEN_STID, &stp, nn);
  3225. if (status)
  3226. goto out;
  3227. oo = openowner(stp->st_stateowner);
  3228. status = nfserr_bad_stateid;
  3229. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3230. goto out;
  3231. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3232. update_stateid(&stp->st_stid.sc_stateid);
  3233. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3234. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3235. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3236. nfsd4_client_record_create(oo->oo_owner.so_client);
  3237. status = nfs_ok;
  3238. out:
  3239. if (!cstate->replay_owner)
  3240. nfs4_unlock_state();
  3241. return status;
  3242. }
  3243. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3244. {
  3245. if (!test_access(access, stp))
  3246. return;
  3247. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3248. clear_access(access, stp);
  3249. }
  3250. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3251. {
  3252. switch (to_access) {
  3253. case NFS4_SHARE_ACCESS_READ:
  3254. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3255. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3256. break;
  3257. case NFS4_SHARE_ACCESS_WRITE:
  3258. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3259. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3260. break;
  3261. case NFS4_SHARE_ACCESS_BOTH:
  3262. break;
  3263. default:
  3264. WARN_ON_ONCE(1);
  3265. }
  3266. }
  3267. static void
  3268. reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
  3269. {
  3270. int i;
  3271. for (i = 0; i < 4; i++) {
  3272. if ((i & deny) != i)
  3273. clear_deny(i, stp);
  3274. }
  3275. }
  3276. __be32
  3277. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3278. struct nfsd4_compound_state *cstate,
  3279. struct nfsd4_open_downgrade *od)
  3280. {
  3281. __be32 status;
  3282. struct nfs4_ol_stateid *stp;
  3283. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3284. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3285. (int)cstate->current_fh.fh_dentry->d_name.len,
  3286. cstate->current_fh.fh_dentry->d_name.name);
  3287. /* We don't yet support WANT bits: */
  3288. if (od->od_deleg_want)
  3289. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3290. od->od_deleg_want);
  3291. nfs4_lock_state();
  3292. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3293. &od->od_stateid, &stp, nn);
  3294. if (status)
  3295. goto out;
  3296. status = nfserr_inval;
  3297. if (!test_access(od->od_share_access, stp)) {
  3298. dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3299. stp->st_access_bmap, od->od_share_access);
  3300. goto out;
  3301. }
  3302. if (!test_deny(od->od_share_deny, stp)) {
  3303. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3304. stp->st_deny_bmap, od->od_share_deny);
  3305. goto out;
  3306. }
  3307. nfs4_stateid_downgrade(stp, od->od_share_access);
  3308. reset_union_bmap_deny(od->od_share_deny, stp);
  3309. update_stateid(&stp->st_stid.sc_stateid);
  3310. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3311. status = nfs_ok;
  3312. out:
  3313. if (!cstate->replay_owner)
  3314. nfs4_unlock_state();
  3315. return status;
  3316. }
  3317. void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
  3318. {
  3319. struct nfs4_openowner *oo;
  3320. struct nfs4_ol_stateid *s;
  3321. if (!so->so_is_open_owner)
  3322. return;
  3323. oo = openowner(so);
  3324. s = oo->oo_last_closed_stid;
  3325. if (!s)
  3326. return;
  3327. if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
  3328. /* Release the last_closed_stid on the next seqid bump: */
  3329. oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
  3330. return;
  3331. }
  3332. oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
  3333. release_last_closed_stateid(oo);
  3334. }
  3335. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3336. {
  3337. unhash_open_stateid(s);
  3338. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3339. }
  3340. /*
  3341. * nfs4_unlock_state() called after encode
  3342. */
  3343. __be32
  3344. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3345. struct nfsd4_close *close)
  3346. {
  3347. __be32 status;
  3348. struct nfs4_openowner *oo;
  3349. struct nfs4_ol_stateid *stp;
  3350. struct net *net = SVC_NET(rqstp);
  3351. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3352. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3353. (int)cstate->current_fh.fh_dentry->d_name.len,
  3354. cstate->current_fh.fh_dentry->d_name.name);
  3355. nfs4_lock_state();
  3356. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3357. &close->cl_stateid,
  3358. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3359. &stp, nn);
  3360. if (status)
  3361. goto out;
  3362. oo = openowner(stp->st_stateowner);
  3363. status = nfs_ok;
  3364. update_stateid(&stp->st_stid.sc_stateid);
  3365. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3366. nfsd4_close_open_stateid(stp);
  3367. release_last_closed_stateid(oo);
  3368. oo->oo_last_closed_stid = stp;
  3369. if (list_empty(&oo->oo_owner.so_stateids)) {
  3370. if (cstate->minorversion) {
  3371. release_openowner(oo);
  3372. cstate->replay_owner = NULL;
  3373. } else {
  3374. /*
  3375. * In the 4.0 case we need to keep the owners around a
  3376. * little while to handle CLOSE replay.
  3377. */
  3378. if (list_empty(&oo->oo_owner.so_stateids))
  3379. move_to_close_lru(oo, SVC_NET(rqstp));
  3380. }
  3381. }
  3382. out:
  3383. if (!cstate->replay_owner)
  3384. nfs4_unlock_state();
  3385. return status;
  3386. }
  3387. __be32
  3388. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3389. struct nfsd4_delegreturn *dr)
  3390. {
  3391. struct nfs4_delegation *dp;
  3392. stateid_t *stateid = &dr->dr_stateid;
  3393. struct nfs4_stid *s;
  3394. __be32 status;
  3395. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3396. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3397. return status;
  3398. nfs4_lock_state();
  3399. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
  3400. cstate->minorversion, nn);
  3401. if (status)
  3402. goto out;
  3403. dp = delegstateid(s);
  3404. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3405. if (status)
  3406. goto out;
  3407. unhash_delegation(dp);
  3408. out:
  3409. nfs4_unlock_state();
  3410. return status;
  3411. }
  3412. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3413. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3414. static inline u64
  3415. end_offset(u64 start, u64 len)
  3416. {
  3417. u64 end;
  3418. end = start + len;
  3419. return end >= start ? end: NFS4_MAX_UINT64;
  3420. }
  3421. /* last octet in a range */
  3422. static inline u64
  3423. last_byte_offset(u64 start, u64 len)
  3424. {
  3425. u64 end;
  3426. WARN_ON_ONCE(!len);
  3427. end = start + len;
  3428. return end > start ? end - 1: NFS4_MAX_UINT64;
  3429. }
  3430. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3431. {
  3432. return (file_hashval(inode) + cl_id
  3433. + opaque_hashval(ownername->data, ownername->len))
  3434. & LOCKOWNER_INO_HASH_MASK;
  3435. }
  3436. /*
  3437. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3438. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3439. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3440. * locking, this prevents us from being completely protocol-compliant. The
  3441. * real solution to this problem is to start using unsigned file offsets in
  3442. * the VFS, but this is a very deep change!
  3443. */
  3444. static inline void
  3445. nfs4_transform_lock_offset(struct file_lock *lock)
  3446. {
  3447. if (lock->fl_start < 0)
  3448. lock->fl_start = OFFSET_MAX;
  3449. if (lock->fl_end < 0)
  3450. lock->fl_end = OFFSET_MAX;
  3451. }
  3452. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3453. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3454. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3455. };
  3456. static inline void
  3457. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3458. {
  3459. struct nfs4_lockowner *lo;
  3460. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3461. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3462. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3463. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3464. if (!deny->ld_owner.data)
  3465. /* We just don't care that much */
  3466. goto nevermind;
  3467. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3468. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3469. } else {
  3470. nevermind:
  3471. deny->ld_owner.len = 0;
  3472. deny->ld_owner.data = NULL;
  3473. deny->ld_clientid.cl_boot = 0;
  3474. deny->ld_clientid.cl_id = 0;
  3475. }
  3476. deny->ld_start = fl->fl_start;
  3477. deny->ld_length = NFS4_MAX_UINT64;
  3478. if (fl->fl_end != NFS4_MAX_UINT64)
  3479. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3480. deny->ld_type = NFS4_READ_LT;
  3481. if (fl->fl_type != F_RDLCK)
  3482. deny->ld_type = NFS4_WRITE_LT;
  3483. }
  3484. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3485. {
  3486. struct nfs4_ol_stateid *lst;
  3487. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3488. return false;
  3489. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3490. struct nfs4_ol_stateid, st_perstateowner);
  3491. return lst->st_file->fi_inode == inode;
  3492. }
  3493. static struct nfs4_lockowner *
  3494. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3495. struct xdr_netobj *owner, struct nfsd_net *nn)
  3496. {
  3497. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3498. struct nfs4_lockowner *lo;
  3499. list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3500. if (same_lockowner_ino(lo, inode, clid, owner))
  3501. return lo;
  3502. }
  3503. return NULL;
  3504. }
  3505. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3506. {
  3507. struct inode *inode = open_stp->st_file->fi_inode;
  3508. unsigned int inohash = lockowner_ino_hashval(inode,
  3509. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3510. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  3511. list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  3512. list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
  3513. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3514. }
  3515. /*
  3516. * Alloc a lock owner structure.
  3517. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3518. * occurred.
  3519. *
  3520. * strhashval = ownerstr_hashval
  3521. */
  3522. static struct nfs4_lockowner *
  3523. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3524. struct nfs4_lockowner *lo;
  3525. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3526. if (!lo)
  3527. return NULL;
  3528. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3529. lo->lo_owner.so_is_open_owner = 0;
  3530. /* It is the openowner seqid that will be incremented in encode in the
  3531. * case of new lockowners; so increment the lock seqid manually: */
  3532. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3533. hash_lockowner(lo, strhashval, clp, open_stp);
  3534. return lo;
  3535. }
  3536. static struct nfs4_ol_stateid *
  3537. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3538. {
  3539. struct nfs4_ol_stateid *stp;
  3540. struct nfs4_client *clp = lo->lo_owner.so_client;
  3541. stp = nfs4_alloc_stateid(clp);
  3542. if (stp == NULL)
  3543. return NULL;
  3544. init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
  3545. list_add(&stp->st_perfile, &fp->fi_stateids);
  3546. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3547. stp->st_stateowner = &lo->lo_owner;
  3548. get_nfs4_file(fp);
  3549. stp->st_file = fp;
  3550. stp->st_access_bmap = 0;
  3551. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3552. stp->st_openstp = open_stp;
  3553. return stp;
  3554. }
  3555. static int
  3556. check_lock_length(u64 offset, u64 length)
  3557. {
  3558. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3559. LOFF_OVERFLOW(offset, length)));
  3560. }
  3561. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3562. {
  3563. struct nfs4_file *fp = lock_stp->st_file;
  3564. int oflag = nfs4_access_to_omode(access);
  3565. if (test_access(access, lock_stp))
  3566. return;
  3567. nfs4_file_get_access(fp, oflag);
  3568. set_access(access, lock_stp);
  3569. }
  3570. static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
  3571. {
  3572. struct nfs4_file *fi = ost->st_file;
  3573. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3574. struct nfs4_client *cl = oo->oo_owner.so_client;
  3575. struct nfs4_lockowner *lo;
  3576. unsigned int strhashval;
  3577. struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
  3578. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
  3579. &lock->v.new.owner, nn);
  3580. if (lo) {
  3581. if (!cstate->minorversion)
  3582. return nfserr_bad_seqid;
  3583. /* XXX: a lockowner always has exactly one stateid: */
  3584. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3585. struct nfs4_ol_stateid, st_perstateowner);
  3586. return nfs_ok;
  3587. }
  3588. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3589. &lock->v.new.owner);
  3590. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3591. if (lo == NULL)
  3592. return nfserr_jukebox;
  3593. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3594. if (*lst == NULL) {
  3595. release_lockowner(lo);
  3596. return nfserr_jukebox;
  3597. }
  3598. *new = true;
  3599. return nfs_ok;
  3600. }
  3601. /*
  3602. * LOCK operation
  3603. */
  3604. __be32
  3605. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3606. struct nfsd4_lock *lock)
  3607. {
  3608. struct nfs4_openowner *open_sop = NULL;
  3609. struct nfs4_lockowner *lock_sop = NULL;
  3610. struct nfs4_ol_stateid *lock_stp;
  3611. struct file *filp = NULL;
  3612. struct file_lock *file_lock = NULL;
  3613. struct file_lock *conflock = NULL;
  3614. __be32 status = 0;
  3615. bool new_state = false;
  3616. int lkflg;
  3617. int err;
  3618. struct net *net = SVC_NET(rqstp);
  3619. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3620. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3621. (long long) lock->lk_offset,
  3622. (long long) lock->lk_length);
  3623. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3624. return nfserr_inval;
  3625. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3626. S_IFREG, NFSD_MAY_LOCK))) {
  3627. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3628. return status;
  3629. }
  3630. nfs4_lock_state();
  3631. if (lock->lk_is_new) {
  3632. struct nfs4_ol_stateid *open_stp = NULL;
  3633. if (nfsd4_has_session(cstate))
  3634. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3635. memcpy(&lock->v.new.clientid,
  3636. &cstate->session->se_client->cl_clientid,
  3637. sizeof(clientid_t));
  3638. status = nfserr_stale_clientid;
  3639. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  3640. goto out;
  3641. /* validate and update open stateid and open seqid */
  3642. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3643. lock->lk_new_open_seqid,
  3644. &lock->lk_new_open_stateid,
  3645. &open_stp, nn);
  3646. if (status)
  3647. goto out;
  3648. open_sop = openowner(open_stp->st_stateowner);
  3649. status = nfserr_bad_stateid;
  3650. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3651. &lock->v.new.clientid))
  3652. goto out;
  3653. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3654. &lock_stp, &new_state);
  3655. } else
  3656. status = nfs4_preprocess_seqid_op(cstate,
  3657. lock->lk_old_lock_seqid,
  3658. &lock->lk_old_lock_stateid,
  3659. NFS4_LOCK_STID, &lock_stp, nn);
  3660. if (status)
  3661. goto out;
  3662. lock_sop = lockowner(lock_stp->st_stateowner);
  3663. lkflg = setlkflg(lock->lk_type);
  3664. status = nfs4_check_openmode(lock_stp, lkflg);
  3665. if (status)
  3666. goto out;
  3667. status = nfserr_grace;
  3668. if (locks_in_grace(net) && !lock->lk_reclaim)
  3669. goto out;
  3670. status = nfserr_no_grace;
  3671. if (!locks_in_grace(net) && lock->lk_reclaim)
  3672. goto out;
  3673. file_lock = locks_alloc_lock();
  3674. if (!file_lock) {
  3675. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3676. status = nfserr_jukebox;
  3677. goto out;
  3678. }
  3679. locks_init_lock(file_lock);
  3680. switch (lock->lk_type) {
  3681. case NFS4_READ_LT:
  3682. case NFS4_READW_LT:
  3683. filp = find_readable_file(lock_stp->st_file);
  3684. if (filp)
  3685. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3686. file_lock->fl_type = F_RDLCK;
  3687. break;
  3688. case NFS4_WRITE_LT:
  3689. case NFS4_WRITEW_LT:
  3690. filp = find_writeable_file(lock_stp->st_file);
  3691. if (filp)
  3692. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3693. file_lock->fl_type = F_WRLCK;
  3694. break;
  3695. default:
  3696. status = nfserr_inval;
  3697. goto out;
  3698. }
  3699. if (!filp) {
  3700. status = nfserr_openmode;
  3701. goto out;
  3702. }
  3703. file_lock->fl_owner = (fl_owner_t)lock_sop;
  3704. file_lock->fl_pid = current->tgid;
  3705. file_lock->fl_file = filp;
  3706. file_lock->fl_flags = FL_POSIX;
  3707. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3708. file_lock->fl_start = lock->lk_offset;
  3709. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3710. nfs4_transform_lock_offset(file_lock);
  3711. conflock = locks_alloc_lock();
  3712. if (!conflock) {
  3713. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3714. status = nfserr_jukebox;
  3715. goto out;
  3716. }
  3717. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  3718. switch (-err) {
  3719. case 0: /* success! */
  3720. update_stateid(&lock_stp->st_stid.sc_stateid);
  3721. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3722. sizeof(stateid_t));
  3723. status = 0;
  3724. break;
  3725. case (EAGAIN): /* conflock holds conflicting lock */
  3726. status = nfserr_denied;
  3727. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3728. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  3729. break;
  3730. case (EDEADLK):
  3731. status = nfserr_deadlock;
  3732. break;
  3733. default:
  3734. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3735. status = nfserrno(err);
  3736. break;
  3737. }
  3738. out:
  3739. if (status && new_state)
  3740. release_lockowner(lock_sop);
  3741. if (!cstate->replay_owner)
  3742. nfs4_unlock_state();
  3743. if (file_lock)
  3744. locks_free_lock(file_lock);
  3745. if (conflock)
  3746. locks_free_lock(conflock);
  3747. return status;
  3748. }
  3749. /*
  3750. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3751. * so we do a temporary open here just to get an open file to pass to
  3752. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3753. * inode operation.)
  3754. */
  3755. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3756. {
  3757. struct file *file;
  3758. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3759. if (!err) {
  3760. err = nfserrno(vfs_test_lock(file, lock));
  3761. nfsd_close(file);
  3762. }
  3763. return err;
  3764. }
  3765. /*
  3766. * LOCKT operation
  3767. */
  3768. __be32
  3769. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3770. struct nfsd4_lockt *lockt)
  3771. {
  3772. struct inode *inode;
  3773. struct file_lock *file_lock = NULL;
  3774. struct nfs4_lockowner *lo;
  3775. __be32 status;
  3776. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3777. if (locks_in_grace(SVC_NET(rqstp)))
  3778. return nfserr_grace;
  3779. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3780. return nfserr_inval;
  3781. nfs4_lock_state();
  3782. status = nfserr_stale_clientid;
  3783. if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid, nn))
  3784. goto out;
  3785. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3786. goto out;
  3787. inode = cstate->current_fh.fh_dentry->d_inode;
  3788. file_lock = locks_alloc_lock();
  3789. if (!file_lock) {
  3790. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3791. status = nfserr_jukebox;
  3792. goto out;
  3793. }
  3794. locks_init_lock(file_lock);
  3795. switch (lockt->lt_type) {
  3796. case NFS4_READ_LT:
  3797. case NFS4_READW_LT:
  3798. file_lock->fl_type = F_RDLCK;
  3799. break;
  3800. case NFS4_WRITE_LT:
  3801. case NFS4_WRITEW_LT:
  3802. file_lock->fl_type = F_WRLCK;
  3803. break;
  3804. default:
  3805. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3806. status = nfserr_inval;
  3807. goto out;
  3808. }
  3809. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
  3810. if (lo)
  3811. file_lock->fl_owner = (fl_owner_t)lo;
  3812. file_lock->fl_pid = current->tgid;
  3813. file_lock->fl_flags = FL_POSIX;
  3814. file_lock->fl_start = lockt->lt_offset;
  3815. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3816. nfs4_transform_lock_offset(file_lock);
  3817. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  3818. if (status)
  3819. goto out;
  3820. if (file_lock->fl_type != F_UNLCK) {
  3821. status = nfserr_denied;
  3822. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  3823. }
  3824. out:
  3825. nfs4_unlock_state();
  3826. if (file_lock)
  3827. locks_free_lock(file_lock);
  3828. return status;
  3829. }
  3830. __be32
  3831. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3832. struct nfsd4_locku *locku)
  3833. {
  3834. struct nfs4_ol_stateid *stp;
  3835. struct file *filp = NULL;
  3836. struct file_lock *file_lock = NULL;
  3837. __be32 status;
  3838. int err;
  3839. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3840. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3841. (long long) locku->lu_offset,
  3842. (long long) locku->lu_length);
  3843. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3844. return nfserr_inval;
  3845. nfs4_lock_state();
  3846. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3847. &locku->lu_stateid, NFS4_LOCK_STID,
  3848. &stp, nn);
  3849. if (status)
  3850. goto out;
  3851. filp = find_any_file(stp->st_file);
  3852. if (!filp) {
  3853. status = nfserr_lock_range;
  3854. goto out;
  3855. }
  3856. file_lock = locks_alloc_lock();
  3857. if (!file_lock) {
  3858. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3859. status = nfserr_jukebox;
  3860. goto out;
  3861. }
  3862. locks_init_lock(file_lock);
  3863. file_lock->fl_type = F_UNLCK;
  3864. file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
  3865. file_lock->fl_pid = current->tgid;
  3866. file_lock->fl_file = filp;
  3867. file_lock->fl_flags = FL_POSIX;
  3868. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3869. file_lock->fl_start = locku->lu_offset;
  3870. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  3871. locku->lu_length);
  3872. nfs4_transform_lock_offset(file_lock);
  3873. /*
  3874. * Try to unlock the file in the VFS.
  3875. */
  3876. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  3877. if (err) {
  3878. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3879. goto out_nfserr;
  3880. }
  3881. /*
  3882. * OK, unlock succeeded; the only thing left to do is update the stateid.
  3883. */
  3884. update_stateid(&stp->st_stid.sc_stateid);
  3885. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3886. out:
  3887. if (!cstate->replay_owner)
  3888. nfs4_unlock_state();
  3889. if (file_lock)
  3890. locks_free_lock(file_lock);
  3891. return status;
  3892. out_nfserr:
  3893. status = nfserrno(err);
  3894. goto out;
  3895. }
  3896. /*
  3897. * returns
  3898. * 1: locks held by lockowner
  3899. * 0: no locks held by lockowner
  3900. */
  3901. static int
  3902. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3903. {
  3904. struct file_lock **flpp;
  3905. struct inode *inode = filp->fi_inode;
  3906. int status = 0;
  3907. lock_flocks();
  3908. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3909. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3910. status = 1;
  3911. goto out;
  3912. }
  3913. }
  3914. out:
  3915. unlock_flocks();
  3916. return status;
  3917. }
  3918. __be32
  3919. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3920. struct nfsd4_compound_state *cstate,
  3921. struct nfsd4_release_lockowner *rlockowner)
  3922. {
  3923. clientid_t *clid = &rlockowner->rl_clientid;
  3924. struct nfs4_stateowner *sop;
  3925. struct nfs4_lockowner *lo;
  3926. struct nfs4_ol_stateid *stp;
  3927. struct xdr_netobj *owner = &rlockowner->rl_owner;
  3928. struct list_head matches;
  3929. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  3930. __be32 status;
  3931. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3932. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  3933. clid->cl_boot, clid->cl_id);
  3934. /* XXX check for lease expiration */
  3935. status = nfserr_stale_clientid;
  3936. if (STALE_CLIENTID(clid, nn))
  3937. return status;
  3938. nfs4_lock_state();
  3939. status = nfserr_locks_held;
  3940. INIT_LIST_HEAD(&matches);
  3941. list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  3942. if (sop->so_is_open_owner)
  3943. continue;
  3944. if (!same_owner_str(sop, owner, clid))
  3945. continue;
  3946. list_for_each_entry(stp, &sop->so_stateids,
  3947. st_perstateowner) {
  3948. lo = lockowner(sop);
  3949. if (check_for_locks(stp->st_file, lo))
  3950. goto out;
  3951. list_add(&lo->lo_list, &matches);
  3952. }
  3953. }
  3954. /* Clients probably won't expect us to return with some (but not all)
  3955. * of the lockowner state released; so don't release any until all
  3956. * have been checked. */
  3957. status = nfs_ok;
  3958. while (!list_empty(&matches)) {
  3959. lo = list_entry(matches.next, struct nfs4_lockowner,
  3960. lo_list);
  3961. /* unhash_stateowner deletes so_perclient only
  3962. * for openowners. */
  3963. list_del(&lo->lo_list);
  3964. release_lockowner(lo);
  3965. }
  3966. out:
  3967. nfs4_unlock_state();
  3968. return status;
  3969. }
  3970. static inline struct nfs4_client_reclaim *
  3971. alloc_reclaim(void)
  3972. {
  3973. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  3974. }
  3975. bool
  3976. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  3977. {
  3978. struct nfs4_client_reclaim *crp;
  3979. crp = nfsd4_find_reclaim_client(name, nn);
  3980. return (crp && crp->cr_clp);
  3981. }
  3982. /*
  3983. * failure => all reset bets are off, nfserr_no_grace...
  3984. */
  3985. struct nfs4_client_reclaim *
  3986. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  3987. {
  3988. unsigned int strhashval;
  3989. struct nfs4_client_reclaim *crp;
  3990. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  3991. crp = alloc_reclaim();
  3992. if (crp) {
  3993. strhashval = clientstr_hashval(name);
  3994. INIT_LIST_HEAD(&crp->cr_strhash);
  3995. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  3996. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  3997. crp->cr_clp = NULL;
  3998. nn->reclaim_str_hashtbl_size++;
  3999. }
  4000. return crp;
  4001. }
  4002. void
  4003. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  4004. {
  4005. list_del(&crp->cr_strhash);
  4006. kfree(crp);
  4007. nn->reclaim_str_hashtbl_size--;
  4008. }
  4009. void
  4010. nfs4_release_reclaim(struct nfsd_net *nn)
  4011. {
  4012. struct nfs4_client_reclaim *crp = NULL;
  4013. int i;
  4014. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4015. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  4016. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  4017. struct nfs4_client_reclaim, cr_strhash);
  4018. nfs4_remove_reclaim_record(crp, nn);
  4019. }
  4020. }
  4021. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  4022. }
  4023. /*
  4024. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4025. struct nfs4_client_reclaim *
  4026. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4027. {
  4028. unsigned int strhashval;
  4029. struct nfs4_client_reclaim *crp = NULL;
  4030. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4031. strhashval = clientstr_hashval(recdir);
  4032. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4033. if (same_name(crp->cr_recdir, recdir)) {
  4034. return crp;
  4035. }
  4036. }
  4037. return NULL;
  4038. }
  4039. /*
  4040. * Called from OPEN. Look for clientid in reclaim list.
  4041. */
  4042. __be32
  4043. nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  4044. {
  4045. struct nfs4_client *clp;
  4046. /* find clientid in conf_id_hashtbl */
  4047. clp = find_confirmed_client(clid, sessions, nn);
  4048. if (clp == NULL)
  4049. return nfserr_reclaim_bad;
  4050. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  4051. }
  4052. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4053. void nfsd_forget_clients(u64 num)
  4054. {
  4055. struct nfs4_client *clp, *next;
  4056. int count = 0;
  4057. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4058. nfs4_lock_state();
  4059. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  4060. expire_client(clp);
  4061. if (++count == num)
  4062. break;
  4063. }
  4064. nfs4_unlock_state();
  4065. printk(KERN_INFO "NFSD: Forgot %d clients", count);
  4066. }
  4067. static void release_lockowner_sop(struct nfs4_stateowner *sop)
  4068. {
  4069. release_lockowner(lockowner(sop));
  4070. }
  4071. static void release_openowner_sop(struct nfs4_stateowner *sop)
  4072. {
  4073. release_openowner(openowner(sop));
  4074. }
  4075. static int nfsd_release_n_owners(u64 num, bool is_open_owner,
  4076. void (*release_sop)(struct nfs4_stateowner *),
  4077. struct nfsd_net *nn)
  4078. {
  4079. int i, count = 0;
  4080. struct nfs4_stateowner *sop, *next;
  4081. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  4082. list_for_each_entry_safe(sop, next, &nn->ownerstr_hashtbl[i], so_strhash) {
  4083. if (sop->so_is_open_owner != is_open_owner)
  4084. continue;
  4085. release_sop(sop);
  4086. if (++count == num)
  4087. return count;
  4088. }
  4089. }
  4090. return count;
  4091. }
  4092. void nfsd_forget_locks(u64 num)
  4093. {
  4094. int count;
  4095. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  4096. nfs4_lock_state();
  4097. count = nfsd_release_n_owners(num, false, release_lockowner_sop, nn);
  4098. nfs4_unlock_state();
  4099. printk(KERN_INFO "NFSD: Forgot %d locks", count);
  4100. }
  4101. void nfsd_forget_openowners(u64 num)
  4102. {
  4103. int count;
  4104. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  4105. nfs4_lock_state();
  4106. count = nfsd_release_n_owners(num, true, release_openowner_sop, nn);
  4107. nfs4_unlock_state();
  4108. printk(KERN_INFO "NFSD: Forgot %d open owners", count);
  4109. }
  4110. static int nfsd_process_n_delegations(u64 num, struct list_head *list)
  4111. {
  4112. int i, count = 0;
  4113. struct nfs4_file *fp, *fnext;
  4114. struct nfs4_delegation *dp, *dnext;
  4115. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4116. list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
  4117. list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
  4118. list_move(&dp->dl_recall_lru, list);
  4119. if (++count == num)
  4120. return count;
  4121. }
  4122. }
  4123. }
  4124. return count;
  4125. }
  4126. void nfsd_forget_delegations(u64 num)
  4127. {
  4128. unsigned int count;
  4129. LIST_HEAD(victims);
  4130. struct nfs4_delegation *dp, *dnext;
  4131. spin_lock(&recall_lock);
  4132. count = nfsd_process_n_delegations(num, &victims);
  4133. spin_unlock(&recall_lock);
  4134. nfs4_lock_state();
  4135. list_for_each_entry_safe(dp, dnext, &victims, dl_recall_lru)
  4136. unhash_delegation(dp);
  4137. nfs4_unlock_state();
  4138. printk(KERN_INFO "NFSD: Forgot %d delegations", count);
  4139. }
  4140. void nfsd_recall_delegations(u64 num)
  4141. {
  4142. unsigned int count;
  4143. LIST_HEAD(victims);
  4144. struct nfs4_delegation *dp, *dnext;
  4145. spin_lock(&recall_lock);
  4146. count = nfsd_process_n_delegations(num, &victims);
  4147. list_for_each_entry_safe(dp, dnext, &victims, dl_recall_lru) {
  4148. list_del(&dp->dl_recall_lru);
  4149. nfsd_break_one_deleg(dp);
  4150. }
  4151. spin_unlock(&recall_lock);
  4152. printk(KERN_INFO "NFSD: Recalled %d delegations", count);
  4153. }
  4154. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4155. /* initialization to perform at module load time: */
  4156. void
  4157. nfs4_state_init(void)
  4158. {
  4159. int i;
  4160. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4161. INIT_LIST_HEAD(&file_hashtbl[i]);
  4162. }
  4163. INIT_LIST_HEAD(&del_recall_lru);
  4164. }
  4165. /*
  4166. * Since the lifetime of a delegation isn't limited to that of an open, a
  4167. * client may quite reasonably hang on to a delegation as long as it has
  4168. * the inode cached. This becomes an obvious problem the first time a
  4169. * client's inode cache approaches the size of the server's total memory.
  4170. *
  4171. * For now we avoid this problem by imposing a hard limit on the number
  4172. * of delegations, which varies according to the server's memory size.
  4173. */
  4174. static void
  4175. set_max_delegations(void)
  4176. {
  4177. /*
  4178. * Allow at most 4 delegations per megabyte of RAM. Quick
  4179. * estimates suggest that in the worst case (where every delegation
  4180. * is for a different inode), a delegation could take about 1.5K,
  4181. * giving a worst case usage of about 6% of memory.
  4182. */
  4183. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4184. }
  4185. static int nfs4_state_create_net(struct net *net)
  4186. {
  4187. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4188. int i;
  4189. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4190. CLIENT_HASH_SIZE, GFP_KERNEL);
  4191. if (!nn->conf_id_hashtbl)
  4192. goto err;
  4193. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4194. CLIENT_HASH_SIZE, GFP_KERNEL);
  4195. if (!nn->unconf_id_hashtbl)
  4196. goto err_unconf_id;
  4197. nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  4198. OWNER_HASH_SIZE, GFP_KERNEL);
  4199. if (!nn->ownerstr_hashtbl)
  4200. goto err_ownerstr;
  4201. nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
  4202. LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
  4203. if (!nn->lockowner_ino_hashtbl)
  4204. goto err_lockowner_ino;
  4205. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  4206. SESSION_HASH_SIZE, GFP_KERNEL);
  4207. if (!nn->sessionid_hashtbl)
  4208. goto err_sessionid;
  4209. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4210. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  4211. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  4212. }
  4213. for (i = 0; i < OWNER_HASH_SIZE; i++)
  4214. INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
  4215. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4216. INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
  4217. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4218. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  4219. nn->conf_name_tree = RB_ROOT;
  4220. nn->unconf_name_tree = RB_ROOT;
  4221. INIT_LIST_HEAD(&nn->client_lru);
  4222. INIT_LIST_HEAD(&nn->close_lru);
  4223. spin_lock_init(&nn->client_lock);
  4224. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  4225. get_net(net);
  4226. return 0;
  4227. err_sessionid:
  4228. kfree(nn->lockowner_ino_hashtbl);
  4229. err_lockowner_ino:
  4230. kfree(nn->ownerstr_hashtbl);
  4231. err_ownerstr:
  4232. kfree(nn->unconf_id_hashtbl);
  4233. err_unconf_id:
  4234. kfree(nn->conf_id_hashtbl);
  4235. err:
  4236. return -ENOMEM;
  4237. }
  4238. static void
  4239. nfs4_state_destroy_net(struct net *net)
  4240. {
  4241. int i;
  4242. struct nfs4_client *clp = NULL;
  4243. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4244. struct rb_node *node, *tmp;
  4245. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4246. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  4247. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4248. destroy_client(clp);
  4249. }
  4250. }
  4251. node = rb_first(&nn->unconf_name_tree);
  4252. while (node != NULL) {
  4253. tmp = node;
  4254. node = rb_next(tmp);
  4255. clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
  4256. rb_erase(tmp, &nn->unconf_name_tree);
  4257. destroy_client(clp);
  4258. }
  4259. kfree(nn->sessionid_hashtbl);
  4260. kfree(nn->lockowner_ino_hashtbl);
  4261. kfree(nn->ownerstr_hashtbl);
  4262. kfree(nn->unconf_id_hashtbl);
  4263. kfree(nn->conf_id_hashtbl);
  4264. put_net(net);
  4265. }
  4266. int
  4267. nfs4_state_start_net(struct net *net)
  4268. {
  4269. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4270. int ret;
  4271. /*
  4272. * FIXME: For now, we hang most of the pernet global stuff off of
  4273. * init_net until nfsd is fully containerized. Eventually, we'll
  4274. * need to pass a net pointer into this function, take a reference
  4275. * to that instead and then do most of the rest of this on a per-net
  4276. * basis.
  4277. */
  4278. if (net != &init_net)
  4279. return -EINVAL;
  4280. ret = nfs4_state_create_net(net);
  4281. if (ret)
  4282. return ret;
  4283. nfsd4_client_tracking_init(net);
  4284. nn->boot_time = get_seconds();
  4285. locks_start_grace(net, &nn->nfsd4_manager);
  4286. nn->grace_ended = false;
  4287. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  4288. nfsd4_grace, net);
  4289. queue_delayed_work(laundry_wq, &nn->laundromat_work, nfsd4_grace * HZ);
  4290. return 0;
  4291. }
  4292. /* initialization to perform when the nfsd service is started: */
  4293. int
  4294. nfs4_state_start(void)
  4295. {
  4296. int ret;
  4297. ret = set_callback_cred();
  4298. if (ret)
  4299. return -ENOMEM;
  4300. laundry_wq = create_singlethread_workqueue("nfsd4");
  4301. if (laundry_wq == NULL) {
  4302. ret = -ENOMEM;
  4303. goto out_recovery;
  4304. }
  4305. ret = nfsd4_create_callback_queue();
  4306. if (ret)
  4307. goto out_free_laundry;
  4308. set_max_delegations();
  4309. return 0;
  4310. out_free_laundry:
  4311. destroy_workqueue(laundry_wq);
  4312. out_recovery:
  4313. return ret;
  4314. }
  4315. /* should be called with the state lock held */
  4316. void
  4317. nfs4_state_shutdown_net(struct net *net)
  4318. {
  4319. struct nfs4_delegation *dp = NULL;
  4320. struct list_head *pos, *next, reaplist;
  4321. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4322. cancel_delayed_work_sync(&nn->laundromat_work);
  4323. locks_end_grace(&nn->nfsd4_manager);
  4324. INIT_LIST_HEAD(&reaplist);
  4325. spin_lock(&recall_lock);
  4326. list_for_each_safe(pos, next, &del_recall_lru) {
  4327. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4328. if (dp->dl_stid.sc_client->net != net)
  4329. continue;
  4330. list_move(&dp->dl_recall_lru, &reaplist);
  4331. }
  4332. spin_unlock(&recall_lock);
  4333. list_for_each_safe(pos, next, &reaplist) {
  4334. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4335. unhash_delegation(dp);
  4336. }
  4337. nfsd4_client_tracking_exit(net);
  4338. nfs4_state_destroy_net(net);
  4339. }
  4340. void
  4341. nfs4_state_shutdown(void)
  4342. {
  4343. destroy_workqueue(laundry_wq);
  4344. nfsd4_destroy_callback_queue();
  4345. }
  4346. static void
  4347. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4348. {
  4349. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4350. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4351. }
  4352. static void
  4353. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4354. {
  4355. if (cstate->minorversion) {
  4356. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4357. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4358. }
  4359. }
  4360. void
  4361. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4362. {
  4363. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4364. }
  4365. /*
  4366. * functions to set current state id
  4367. */
  4368. void
  4369. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4370. {
  4371. put_stateid(cstate, &odp->od_stateid);
  4372. }
  4373. void
  4374. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4375. {
  4376. put_stateid(cstate, &open->op_stateid);
  4377. }
  4378. void
  4379. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4380. {
  4381. put_stateid(cstate, &close->cl_stateid);
  4382. }
  4383. void
  4384. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4385. {
  4386. put_stateid(cstate, &lock->lk_resp_stateid);
  4387. }
  4388. /*
  4389. * functions to consume current state id
  4390. */
  4391. void
  4392. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4393. {
  4394. get_stateid(cstate, &odp->od_stateid);
  4395. }
  4396. void
  4397. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4398. {
  4399. get_stateid(cstate, &drp->dr_stateid);
  4400. }
  4401. void
  4402. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4403. {
  4404. get_stateid(cstate, &fsp->fr_stateid);
  4405. }
  4406. void
  4407. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4408. {
  4409. get_stateid(cstate, &setattr->sa_stateid);
  4410. }
  4411. void
  4412. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4413. {
  4414. get_stateid(cstate, &close->cl_stateid);
  4415. }
  4416. void
  4417. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4418. {
  4419. get_stateid(cstate, &locku->lu_stateid);
  4420. }
  4421. void
  4422. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4423. {
  4424. get_stateid(cstate, &read->rd_stateid);
  4425. }
  4426. void
  4427. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4428. {
  4429. get_stateid(cstate, &write->wr_stateid);
  4430. }