nfs4state.c 132 KB

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